【发布时间】: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)
【问题讨论】:
-
你能举例说明你的输出是什么样的吗?
标签: html python-3.x beautifulsoup