我是 Scrapy 的新手,我想做的是制作一个爬虫,它只会跟踪给定 start_urls
举个例子,假设我只是想让一个爬虫通过 start_urls
设置为 https://www.airbnb.com/s?location=New+York%2C+NY&checkin=&checkout=&guests=1 的 AirBnB 房源。
我不想抓取 URL 中的所有链接,我只想抓取 xpath 中的链接 //*[@id="results"]
目前我正在使用下面的代码来抓取所有的链接,我怎样才能让它只抓取//*[@id="results"]
from scrapy.selector import HtmlXPathSelector
from tutorial.items import DmozItem
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
class BSpider(CrawlSpider):
name = "bt"
#follow = True
allowed_domains = ["mydomain.com"]
start_urls = ["http://myurl.com/path"]
rules =(Rule(SgmlLinkExtractor(allow = ()) ,callback = 'parse_item', follow=True),)
def parse_item(self, response):
{parse code}
任何正确方向的提示将不胜感激, 谢谢!
最佳答案
您可以将 restrict_xpaths 关键字参数传递给 SgmlLinkExtractor。来自 the docs :
- restrict_xpaths(str 或 list)——是一个 XPath(或 XPath 的列表),它定义了响应中应该从中提取链接的区域。如果给定,将仅扫描那些 XPath 选择的文本以查找链接。
关于python - 我如何告诉 Scrapy 只抓取 Xpath 中的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14028258/