Beautifulsoup python获取页面的日期和作者

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Beautifulsoup python获取页面的日期和作者相关的知识,希望对你有一定的参考价值。

我试图从页面的HTML代码接收日期和名称,但我的代码不起作用。我试图从这部分代码中提取。有了这个URL,我应该收到:我的心情和2016年10月2日,但是我收到了一个错误。

我使用type="text/javascript"作为搜索短语,因为页面源的这一部分以:<script type="text/javascript">开头(这部分比我插入的要大得多,但我只需要这两个元素,作者和日期)

我正在提取的代码的HTML部分:

ajax.preload('al_photos.php', {"act":"show","list":"album-68872445_00/rev","photo":"-68872445_422126739","module":"photos"}, ["album-68872445_00/rev",7557,3696.000000,[{"id":"-68872445_422205711","base":"https://pp.userapi.com/","commcount":0,"commshown":0,"comments":"<div id="pv_comments" class="pv_comments wall_module">
  <div id="pv_comments_header" onclick="Photoview.comments();" class="pv_comments_header unshown"></div>
  <div id="pv_comments_list" class="pv_comments_list  unshown"></div>
  <div class="pv_no_commments_placeholder_wrap">
    <div class="pv_no_commments_placeholder no_rows unshown">Будьте первым, кто оставит комментарий к этой фотографии.</div>
    <div class="pv_closed_commments_placeholder no_rows ">Возможность комментирования этой фотографии ограничена.</div>
  </div>
</div>","reply_form":"","reply_options":[],"date":"<span class="rel_date">10 июл 2016</span>","tags":[0],"tagged":[],"album":"<a href="/album-68872445_00" onclick="return nav.go(this, event)">Фотографии на стене сообщества</a>","author":"<a href="/lovely_detka_tytyty" class="group_link">my mood</a>"

我的代码:

from bs4 import BeautifulSoup
import requests
import lxml
import json
url = 'https://vk.com/photo-68872445_422126739?rev=1'
req = requests.get(url)
soup = BeautifulSoup(req.text, 'lxml')
data = soup.select('type="text/javascript"')[0]
name = json.loads(data.text)["author"]
date = json.loads(data.text)["date"]
print (name)
print (date)

screenshot of page source

答案

我不确定它会对你有所帮助,因为我无法在script标签中看到你的数据。但是,如果您的最终目的是获取日期和作者,请参阅以下代码:

from bs4 import BeautifulSoup
import requests
import lxml
import json
url = 'https://vk.com/photo-68872445_422126739?rev=1'
req = requests.get(url)
soup = BeautifulSoup(req.text, 'lxml')

dls = soup.find_all("dl",{'class':'si_row'})
for dl in dls:
    atag = dl.find('a')
    if atag:
        author_link = atag.get('href')
        author_name = atag.get_text()
        print(author_link)
        print(author_name)

span_date = soup.find('span',{'class':'item_date'})
if span_date:
    date = span_date.get_text()
    print(date)

编辑:

对于记录,您的错误可能是因为您正在使用requests获取页面,而您正在搜索的数据位于ajax响应中。如果您想从脚本中获取更多数据,可以查看selenium

Selenium documentation

另一答案

使用selenium返回日期和作者

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = 'https://vk.com/photo-68872445_422126739'
driver = webdriver.Chrome()
driver.get(url)
item =  WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".group_link"))).text
item2 = driver.find_element_by_css_selector('.rel_date').text
print(item, item2)
driver.quit()

以上是关于Beautifulsoup python获取页面的日期和作者的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫如何获取页面内所有URL链接?本文详解

python 使用pycurl的cURL Fitbit社区组,使用BeautifulSoup刮取页面,发送给Redis(写入获取数据以创建Fitbit仪表板

BeautifulSoup:获取特定表的内容

Beautifulsoup4 没有返回页面上的所有链接

Python网络爬虫——BeautifulSoup4库的使用

Python:使用 BeautifulSoup 库抓取百度天气