python - 从链接数组中抓取 HTML

标签 python selenium web-scraping beautifulsoup python-requests

我拼凑了一个脚本,该脚本可以在产品搜索页面上抓取各个产品页面,并收集产品完整描述的标题/价格/链接。它是使用循环开发的,并向每个页面添加 +i (www.exmple.com/search/laptops?page=(1+i)),直到出现 200 错误。

产品标题包含指向实际产品完整描述的链接 - 我现在想“访问”该链接并从产品的完整描述中抓取主要数据。

我为从产品搜索页面提取的链接构建了一个数组 - 我猜运行这将是一个很好的起点。

我如何从数组中的链接中提取 HTML(即访问单个产品页面并获取实际产品数据,而不仅仅是产品搜索页面的摘要)?

以下是我获得的 CSV 格式的当前结果:

 Link                                Title                 Price
 example.com/laptop/product1        laptop                 £400
 example.com/laptop/product2        laptop                 £400
 example.com/laptop/product3        laptop                 £400
 example.com/laptop/product4        laptop                 £400
 example.com/laptop/product5        laptop                 £400

最佳答案

首先获取所有页面链接。然后迭代该列表并从各个页面获取您需要的任何信息。我在这里只检索规范值。您可以执行任何您想要的值。

from bs4 import BeautifulSoup
import requests
all_links=[]
url="https://www.guntrader.uk/dealers/street/ivythorn-sporting/guns?page={}"
for page in range(1,3):
  res=requests.get(url.format(page)).text
  soup=BeautifulSoup(res,'html.parser')
  for link in soup.select('a[href*="/dealers/street"]'):
      all_links.append("https://www.guntrader.uk" + link['href'])

print(len(all_links))
for a_link in all_links:
    res = requests.get(a_link).text
    soup = BeautifulSoup(res, 'html.parser')
    if soup.select_one('div.gunDetails'):
      print(soup.select_one('div.gunDetails').text)

每个页面的输出都是这样的。

Specifications

Make:Schultz & Larsen
Model:VICTORY GRADE 2 SPIRAL-FLUTED
Licence:Firearm
Orient.:Right Handed
Barrel:23"
Stock:14"
Weight:7lb.6oz.
Origin:Other
Circa:2017
Cased:Makers-Plastic
Serial #:DK-V11321/P20119
Stock #:190912/002
Condition:Used



Specifications

Make:Howa
Model:1500 MINI ACTION [ 1-7'' ] MDT ORYX CHASSIS
Licence:Firearm
Orient.:Right Handed
Barrel:16"
Stock:13 ½"
Weight:7lb.15oz.
Origin:Other
Circa:2019
Cased:Makers-Plastic
Serial #:B550411
Stock #:190905/002
Condition:New



Specifications

Make:Weihrauch
Model:HW 35
Licence:No Licence
Orient.:Right Handed
Scope:Simmons 3-9x40
Total weight:9lb.3oz.
Origin:German
Circa:1979
Serial #:746753
Stock #:190906/004
Condition:Used
<小时/>

如果您想从每个链接获取标题和价格。试试这个。

from bs4 import BeautifulSoup
import requests
all_links=[]
url="https://www.guntrader.uk/dealers/street/ivythorn-sporting/guns?page={}"
for page in range(1,3):
  res=requests.get(url.format(page)).text
  soup=BeautifulSoup(res,'html.parser')
  for link in soup.select('a[href*="/dealers/street"]'):
      all_links.append("https://www.guntrader.uk" + link['href'])

print(len(all_links))
for a_link in all_links:
    res = requests.get(a_link).text
    soup = BeautifulSoup(res, 'html.parser')
    if soup.select_one('h1[itemprop="name"]'):
      print("Title:" + soup.select_one('h1[itemprop="name"]').text)
      print("Price:" + soup.select_one('p.price').text)

关于python - 从链接数组中抓取 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029329/

相关文章:

python - 如何在不转换为 int 的情况下获取字节串中的单个字节

selenium - By.id、By.className 和 By.xpath 的比较只给出了一个高级父 ID 和一个 child 的 className

python - 压缩列表的递归方法?

java - 数据提供者和断言

Selenium 点击链接

python - BeautifulSoup ,get_text 但不是 <span> 文本..我怎样才能得到它?

网页抓取时的 R 编码问题 - 如何修复损坏的文本?

javascript - 从 php url 获取可下载二进制文件的文件名,而不实际下载文件

python - 寻找图书馆阅读 iPod 的数据库

python - 使用 pandas DataReader 获取 "Adj Close"