【问题标题】:Beautifulsoup python get date and author of a pageBeautifulsoup python获取页面的日期和作者
【发布时间】:2019-03-14 09:11:15
【问题描述】:

我正在尝试从页面的 html 代码中接收日期和名称,但我的代码不起作用。我正在尝试从这部分代码中提取。使用此 URL,我应该收到:我的心情和 10 июл 2016,但我收到错误消息。

我使用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\">\n  <div id=\"pv_comments_header\" onclick=\"Photoview.comments();\" class=\"pv_comments_header unshown\"><\/div>\n  <div id=\"pv_comments_list\" class=\"pv_comments_list  unshown\"><\/div>\n  <div class=\"pv_no_commments_placeholder_wrap\">\n    <div class=\"pv_no_commments_placeholder no_rows unshown\">Будьте первым, кто оставит комментарий к этой фотографии.<\/div>\n    <div class=\"pv_closed_commments_placeholder no_rows \">Возможность комментирования этой фотографии ограничена.<\/div>\n  <\/div>\n<\/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

【问题讨论】:

  • 你能举例说明你的输出是什么样的吗?

标签: html python-3.x beautifulsoup


【解决方案1】:

我不确定它是否会对您有所帮助,因为我无法在 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

【讨论】:

  • 它工作得很好,但是你是怎么找到 'class':'si_row' 和 'class':'item_date' 的?当我在 chrome 中查看“查看页面源代码”时,我找不到这些类。它是什么魔法?它是如何工作的?以及为什么您不使用像这样的另一个 URL,例如:vk.com/wall-68872445_34141。你在哪里找到这些课程的?
【解决方案2】:

使用 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 2013-01-29
    • 2020-07-26
    • 2015-12-21
    • 1970-01-01
    • 2014-12-17
    • 2015-06-07
    相关资源
    最近更新 更多