【问题标题】:Unable to grab a field in Web Scraping Python无法在 Web Scraping Python 中抓取字段
【发布时间】:2021-09-18 21:47:30
【问题描述】:

我正在尝试从以下站点获取所有公司名称(突出显示)。这是我的第一个网络抓取工作,所以我正在努力理解为什么我无法获取公司名称,尽管我有正确的参数,

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'} # This is chrome, you can set whatever browser you like
request = requests.get("https://www.hispanicmeetings.org", verify=False, headers=headers)
soup = bs4.BeautifulSoup(request.content)
soup.find_all("a href") # this is not getting me company names
soup.find_all('alt') #this either

我在找到网页的 html 标签的帮助下尝试了许多小组合,但似乎没有任何效果。任何将所有公司名称都放在一个地方的建议对我来说意义重大。

【问题讨论】:

  • 调用url 并遍历page

标签: javascript python python-3.x web-scraping beautifulsoup


【解决方案1】:

您没有使用 BeautifulSoup 正确引用正确的标签和/或属性。我建议找一个关于 html 的小教程来理解标签和属性,然后看看你如何用 bs4 选择它们。然后您可以查看如何提取标签,并从这些标签中提取文本和/或属性值。试试下面的代码:

import requests
import bs4

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'} # This is chrome, you can set whatever browser you like
request = requests.get("https://www.cloudtango.org", verify=False, headers=headers)
soup = bs4.BeautifulSoup(request.content, 'html.parser')
data = soup.find_all('td', {'class':'company'})

for each in data:
    print(each.find('img')['alt'])

输出:

Managed Solution
Redcentric
First Focus
K3 Technology
ICC Managed Services
AffinityMSP
BCA IT, Inc.
CloudCoCo Plc (formerly Adept4 PLC)
SCC
Datacom Systems
Compugen
Cancom
All Covered
Computacenter
q.beyond AG
Atos
Controlware GmbH Firmenzentrale
Trustmarque
Bytes
AHEAD
ACP IT Solutions GmbH
PROFI Engineering Systems AG
PQR
Orbit GmbH
SVA System Vertrieb Alexander GmbH
Ensono
Phoenix Software Ltd
Atea Norge AS
Axians
Kick ICT Group
Atea Sverige AB
Catapult Systems LLC
Valid

【讨论】:

  • 非常感谢!!但是为什么在代码中的两个不同标签内有“image”和“alt”呢?它们似乎在 html ( ) 内的一个标签下
  • 如果你有时间,找一个初学者的html教程。标签是<img>,alt是<img>标签中的一个属性。
  • 知道了!非常感谢
  • 别担心!很高兴为您提供帮助!
【解决方案2】:

公司名称作为img 标记的alt 属性存在于<td> 标记内,类名为公司。

您正在使用soup.find_all('alt') - alt 不是标签。 汤对象只能选择 HTML 标签,属性不能选择。

import requests
import bs4

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'} # This is chrome, you can set whatever browser you like
request = requests.get("https://www.cloudtango.org", verify=False, headers=headers)
soup = bs4.BeautifulSoup(request.content, 'html.parser')

t = soup.findAll('td', class_='company')

for i in t:
    print(i.find('img')['alt'])

Output:

First Focus
Managed Solution
centrexIT
Carbon60
Redcentric
BlackPoint IT Services
.
.
.

【讨论】:

  • 谢谢你,Ram 但是你为什么在两个不同的标签中使用 ('img')['alt']) ?就像在 html 中为什么不是一个?
  • <img> 是一个标签,alt 是img 标签的一个属性。在我的代码中,我选择了存在于img 中的alt。并且您正在选择 alt 这是一个属性(只能从汤对象中选择 HTML 标签,而不是属性。)
  • 请点赞并接受我的回答。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2021-10-26
  • 1970-01-01
  • 2021-08-19
相关资源
最近更新 更多