空结果集靓汤

2024-05-16 19:28:00 发布

您现在位置:Python中文网/ 问答频道 /正文

从纽约时报网站刮取文章,得到一个空的结果集。我的目标是获取h3项目的url和文本。当我运行这个,我得到一个空集。打印片段说明我走的是正确的道路。。。 目标url-http://query.nytimes.com/search/sitesearch/?action=click&contentCollection&region=TopBar&WT.nav=searchWidget&module=SearchSubmit&pgtype=sectionfront#/san+diego/24hours

url = "http://query.nytimes.com/search/sitesearch/?action=click&contentCollection&region=TopBar&WT.nav=searchWidget&module=SearchSubmit&pgtype=sectionfront{data}"
html = urlopen(url.format(data="#"+'/san+diego/24hours'))

soup = BeautifulSoup(html.read().decode('utf-8'),"lxml")
section = soup.find("ol",class_='searchResultsList flush')
items = section.find_all('li', class_="story")
print items

Tags: comhttpurl目标searchactionqueryregion
1条回答
网友
1楼 · 发布于 2024-05-16 19:28:00

HTML确实不包含数据。查看Chrome开发人员工具中的Network选项卡,可以看到搜索结果是通过AJAX查询获取的,该查询指向以下URL:http://query.nytimes.com/svc/add/v1/sitesearch.json?q=san%20diego&begin_date=24hoursago&facet=true

下面是一个发现的截图:

enter image description here

您必须打开开发人员工具(尝试查看菜单),选择网络选项卡,重新加载页面,然后环顾四周。XHR=XmlHttpRequest现在被称为AJAX请求。这意味着一些Javascript向服务器请求数据。在

这是JSON,所以您实际上很幸运,因为这比解析HTML要好得多。在

相关问题 更多 >