Python 从链接数组中删除HTML

Python 从链接数组中删除HTML,python,selenium,web-scraping,beautifulsoup,python-requests,Python,Selenium,Web Scraping,Beautifulsoup,Python Requests,我拼凑了一个脚本,可以在产品搜索页面上刮取产品的各个页面,并收集产品完整描述的标题/价格/链接。它是使用循环开发的,并在每个页面(www.example.com/search/laptops?page=(1+i))上添加一个+i,直到应用了200个错误 产品标题包含实际产品完整描述的链接-我现在想“访问”该链接,并从产品完整描述中获取主要数据 我为从产品搜索页面提取的链接构建了一个数组——我猜这将是一个很好的起点 我如何从数组中的链接中提取HTML(即访问单个产品页面,获取实际的产品数据,而不仅

我拼凑了一个脚本,可以在产品搜索页面上刮取产品的各个页面,并收集产品完整描述的标题/价格/链接。它是使用循环开发的,并在每个页面(www.example.com/search/laptops?page=(1+i))上添加一个
+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

只需从项目标题中提取作为URL的字符串部分。 做一个:

并将此html作为xml树格式进行迭代。您可以在请求和beautifulsoup上参考此易于查找的链接:


希望这有帮助?我不确定你的问题是否正确,但这里的任何内容都可以通过urlib2/requests/beautifulSoup/json/xml python库在处理web抓取/解析时完成

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

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)

你能分享你的网址吗-这是我从中抓取示例数据的页面(使用笔记本电脑作为通用示例),但它是指向完整数据的链接,我只想抓取一个供参考的-可以递归地从框中抓取页面-这可能有助于确保网站所有者对其无异议。嗨,昆都克-好人,我回家后会看看这个!我真的没想到会有一个完整的“给你”,所以谢谢你有值得一读的文件吗?看看你的代码,它实际上与我在产品搜索页面上已经看到的产品页面非常相似。我只从官方文档中了解到:@AndrewGlass:当然。一旦你运行代码,请告诉我你那边的状态。是的,工作很好-记得车里有笔记本电脑,所以它跑了下来,然后把它推了出去。100%-几乎不需要做什么更改,比如将数据推送到csv,并添加一些东西,但在你走出去时运行,正如我所说的,所以宾果好男人!需要对它做更多的阅读,做一些测试和玩弄,但可能已经得到了上面的确切答案-显然,我们将通过它,并确保了解所提供的文档正在发生什么。干杯
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)