我在使用Scrapy时遇到了一些XPath问题。
我正在查看一个表格中的链接 - 在浏览器中,查看元素时会列出完整的链接。然而,Scrapy shell却截断了链接的末尾。
来自表格的示例链接:
http://www.ashp.org/DrugShortages/Current/Bulletin.aspx?id=463
检查元素时:
<a href="/DrugShortages/Current/Bulletin.aspx?id=463">
在 scrapy shell 中提取会移除 463。
有什么想法吗?
这是爬虫的代码。实际上还没有设置它来通过链接进行爬取,我认为首先应该将所有正确的 XPath 语法设置好。
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from ashp.items import AshpItem
class MySpider(BaseSpider):
name = "ashp"
allowed_domains = ["ashp.org"]
start_urls = ["http://ashp.org/menu/DrugShortages/CurrentShortages"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//span[@class='pl']")
for titles in titles:
title = titles.select("a/text()").extract()
link = titles.select("a/@href").extract()
print title, link