python - 试图从网络上获取链接
问题描述
该代码仅获得 1 个 URL 列表然后停止。它应该得到209个项目。为什么它只打印一行?
def parse_link(self, response):
# Extract the list of products
results = response.xpath('//li[@class="s-item "]')
# Extract info for each product
for product in results:
name = product.xpath('//*[@class="s-item__link"]').extract_first()[30:124]
yield{
"Name":name,
}
解决方案
您的缩进不正确(您需要yield
在for
循环内移动):
def parse_link(self, response):
# Extract the list of products
results = response.xpath('//li[@class="s-item "]')
# Extract info for each product
for product in results:
name = product.xpath('//*[@class="s-item__link"]').extract_first()[30:124]
yield {"Name":name,}
推荐阅读
- ninject - Ninject级联命名解析?
- java - Have a Queue subscribed to multiple Queues, programmaticaly
- vb.net - 从文本文件中每隔四行读取一次
- html - 什么是现代的@font-face 声明?
- mongoose - 猫鼬中的 findByIdAndRemove 和 findByIdAndDelete 有什么区别?
- azure - 如果 X% 的请求失败,Azure Web App Service 会触发警报
- string - Arduino serial data manipulation - Sensors Serial Data, Read and parse to variables
- python - 使用 PyQt5/QtDesigner 弹出窗口或多个窗口
- rest - What should be the Rest URL for the action "Move the competitor from team1 to to team2"
- javascript - Replacing multiple strings of text on webpage using javascript