【发布时间】:2021-07-13 13:13:34
【问题描述】:
我正在使用 html 请求和漂亮的汤(我是新手)开发一个网络爬虫。对于 1 个网页 (https://www.selfridges.com/GB/en/cat/beauty/make-up/?pn=1),我正在尝试抓取产品网格中每个产品的链接。我尝试过使用 absolute_links 和 xpath:
session = HTMLSession()
for x in range(1, 30):
url = f'https://www.selfridges.com/GB/en/cat/beauty/make-up/?pn={x}'
r = session.get(url)
r.html.render(sleep=2)
products = r.html.xpath('//*[@id="content"]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[6]/div/div/div[1]/div/div/div/div[2]', first=True)
productlist = products.absolute_links
productlinks.extend(productlist)
print(productlinks)
和BeautifulSoup:
session = HTMLSession()
for x in range(1, 30):
url = f'https://www.selfridges.com/GB/en/cat/beauty/make-up/?pn={x}'
r = session.get(url)
r.html.render(sleep=2)
soup = BeautifulSoup(r.content, 'lxml')
productlist = r.html.find('div', class_="listing-items c-listing-items initialized")
print(productlist)
for item in productlist:
for link in item.find_all('a', href=True):
productlinks.append(baseurl + link['href'])
print(productlinks)
两者都返回空列表或AttributeError: 'NoneType' object has no attribute 'absolute_links'。我不确定为什么会这样。任何帮助将不胜感激。
【问题讨论】:
标签: python html web-scraping beautifulsoup request