我正在尝试使用Rule
和LinkExtractor
来提取链接,这是我在scrapy shell中的代码
from urllib.parse import quote
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
url= f'https://www.google.com/search?q={quote("Hello World")}'
fetch(url)
x=LinkExtractor(restrict_xpaths='//div[@class="r"]/a')
y=Rule(x)
我尝试使用dir(x)
来查看我可以应用哪些方法,我能找到的最好的方法是x.__sizeof__()
,但它显示的是32实际 10 个链接。
我的问题是如何找出使用它们实际提取的链接(类似列表)。
这就是 dir(x)
显示的内容
['__class__'、'__delattr__'、'__dict__'、'__dir__'、'__doc__'、'__eq__'、'__format__'、'__ge__'、'__getattribute__'、'__gt__'、'__hash__' , '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', ' __str__'、'__subclasshook__'、'__weakref__'、'_csstranslator'、'_extract_links'、'_link_allowed'、'_process_links'、'allow_domains'、'allow_res'、'canonicalize'、'deny_domains'、'deny_extensions'、'deny_res' , 'extract_links', 'link_extractor', '匹配', 'restrict_xpaths']
最佳答案
您可以使用以下方法来准确获取提取的内容
x=LinkExtractor(restrict_xpaths='//div[@class="r"]/a')
links_objects=x.extract_links(response) # a list like
对于您可以使用的实际网址
for link in links_objects:
print(link.url) #links
关于python - 如何知道scrapy规则提取了哪些链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959097/