刮壳罐';当xpath在Chrome控制台中工作时,不能抓取信息
我正在做一个收集大学教授联系信息的项目。(因此它不是恶意的。) 教授页面是动态的。我通过Chrome网络找到了这个请求。然而,当scrapy xpath在浏览器上工作时,它在scrapy shell中不工作。我甚至尝试添加标题。 测试版本:刮壳罐';当xpath在Chrome控制台中工作时,不能抓取信息,xpath,scrapy,web-crawler,Xpath,Scrapy,Web Crawler,我正在做一个收集大学教授联系信息的项目。(因此它不是恶意的。) 教授页面是动态的。我通过Chrome网络找到了这个请求。然而,当scrapy xpath在浏览器上工作时,它在scrapy shell中不工作。我甚至尝试添加标题。 测试版本: import scrapy from universities.items import UniversitiesItem class UniversityOfHouston(scrapy.Spider): #name = 'Universi
import scrapy
from universities.items import UniversitiesItem
class UniversityOfHouston(scrapy.Spider):
#name = 'University_of_Houston'
name = 'uh2'
allowed_domains = ['uh.edu']
start_urls = ['http://www.uh.edu/directory/']
def __init__(self):
self.last_name = ''
def parse(self, response):
with open('kw.txt') as file_object:
last_names = file_object.readlines()
for ln in ['Lee', 'Zhao']:
self.last_name = ln.strip()
print('-----------------------------------------------------')
print("scraping last name: ", self.last_name)
query = "http://www.uh.edu/directory/proxy.php?q=" + self.last_name + \
"&submit=Search&limit=250&loc=HR730&pos=faculty%7Cstaff&faculty=faculty&staff=staff&student=student"
yield scrapy.Request(query, callback=self.parse_staff)
def parse_staff(self, response):
results = response.xpath('//dt/a/@href').extract()
for result in results:
query_proxy = 'http://www.uh.edu/directory/' + result.replace("index.php", "proxy.php")
yield scrapy.Request(query_proxy, callback=self.parse_item)
def parse_item(self, response):
full_name = response.xpath('//h2[@class="single_title"]/text()').extract_first()
if full_name:
if self.last_name in full_name.split():
item = UniversitiesItem()
item['fullname'] = full_name
# last_name = full_name.split()[-1]
# item['lastname'] = last_name
# item['firstname'] = full_name[:-len(last_name)].strip()
item['university'] = 'University of Houston'
try:
item['department'] = response.xpath('//td/a[@class="org"]/text()').extract_first()
item['title'] = response.xpath('//tr/td[@class="title"]/text()').extract_first()
item['email'] = response.xpath('//td/a[@title="email address"]/text()').extract_first()
item['phone'] = response.xpath('//td[@class="tel"]/a/text()').extract_first()
except ValueError:
pass
yield item
问题是因为数据是使用网页上的AJAX调用获取的。当您获取主页面时,数据不可用 将您的
parse_staff
函数更改为以下,它应该可以工作
def parse_staff(self, response):
results = response.xpath('//dt/a/@href').extract()
for result in results:
query = 'http://www.uh.edu/directory/' + result
query_proxy = "https://ssl.uh.edu/directory/" + result.replace("index.php", "proxy.php")
yield response.follow(query_proxy, callback=self.parse_item)
打开firefox,转到
about:config
searchjavascript.enabled
并将其设置为false。然后在firefox中打开页面。如果您没有看到所需的数据,那么页面将使用javascript脚本,而scrapy就不行。你需要使用Scrapy+Splash组合,或者你需要使用Scrapy+SeleniumI,因为没有firefox,但我会试试。我以前用过Scrapy+硒。但它返回错误:selenium.common.exceptions.StaleElementReferenceException。我将延迟设置为20秒,但仍然不起作用。我正在考虑使用Scrapy+Phantomjs.Post你的代码question@Tarun拉尔瓦尼发帖了。哇,真管用。但query\u代理不应该从开始吗?你在哪里找到的?在chrome开发者工具,网络标签。你应该在抓取之前检查页面正在做什么是的,这是我在“网络”选项卡中看到的。页面然后调用此URL,因此我们可以跳过此页面,直接转到获取数据的页面。这是另一个问题。我的脚本只能在结果中保存一项(我试图将其保存到csv文件中)。你能找出原因吗?我刚刚发布了一个测试版本。
def parse_staff(self, response):
results = response.xpath('//dt/a/@href').extract()
for result in results:
query = 'http://www.uh.edu/directory/' + result
query_proxy = "https://ssl.uh.edu/directory/" + result.replace("index.php", "proxy.php")
yield response.follow(query_proxy, callback=self.parse_item)