我无法弄清楚我在代码中遇到的任何错误。X Paths没有问题。如果单独检查功能,功能的功能很好。当我运行蜘蛛会出现以下错误。基本上,它在到达时发生层2函数要处理。当我首先签到我可以注意到这些链接是成熟的URL。我该怎么办此刻获得结果。预先感谢。
蜘蛛:
import requests
from lxml import html
def Startpoint():
address = "https://www.sephora.ae/en/stores/"
page = requests.get(address)
tree = html.fromstring(page.text)
titles=tree.xpath('//li[contains(@class,"level0")]')
for title in titles:
href = title.xpath('.//a[contains(@class,"level0")]/@href')[0]
Layer2(href)
def Layer2(address):
page = requests.get(address)
tree = html.fromstring(page.text)
titles=tree.xpath('//div[@class="product-manufacturer"]')
for title in titles:
href = title.xpath('.//a/@href')[0]
Endpoint(href)
def Endpoint(address):
page = requests.get(address)
tree = html.fromstring(page.text)
titles=tree.xpath('//div[@class="add-to-cart"]')
for title in titles:
Name = title.xpath('.//div[@class="h2"]/text()')[0]
Price = title.xpath('.//span[@class="price"]/text()')[0]
print('{}{}'.format(Name, Price))
Startpoint()
根据Max Paymar的建议修改上述代码。现在它正在工作。
我从未使用过此库,所以我可能是错误的,但是看起来URL变量需要修改,以便它是字符串。括号'[在错误消息中肯定是不合适的。