如果在javascript中返回,如何刮取搜索结果(使用python)
问题描述:
我想要抓取的网站使用JavaScript填充返回。
The site I want to scrape populates returns using JavaScript.
我可以简单地以某种方式调用脚本并使用其结果吗? (当然,没有分页。)我不想运行整个过程来抓取生成的格式化HTML,但原始源是空白的。
Can I simply call the script somehow and work with its results? (Then without pagination, of course.) I don't want to run the entire thing to scrape the resulting formatted HTML, but the raw source is blank.
有一个看: http://kozbeszerzes.ceu.hu/searchresults.xhtml?q = 1998& page = 0
回报的来源只是
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/templates/base_template.xsl"?>
<content>
<head>
<SCRIPT type="text/javascript" src="/js/searchResultsView.js"></SCRIPT>
</head>
<whitebox>
<div id = "hits"></div>
</whitebox>
</content>
我更喜欢简单的Python工具。
I would prefer simple Python tools.
答
我下载了 Selenium 和 ChromeDriver 。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://kozbeszerzes.ceu.hu/searchresults.xhtml?q=1998&page=0')
for e in driver.find_elements_by_class_name('result'):
link = e.find_element_by_tag_name('a')
print(link.text.encode('ascii', 'ignore'), link.get_attribute('href').encode('ascii', 'ignore'))
driver.quit()
如果您使用的是Chrome,则可以使用F12检查页面属性,这非常有用。
If you're using Chrome, you can inspect the page attributes using F12, which is pretty useful.