【问题标题】:Extracting company name and other information inside all urls present in a webpage using beautifulsoup使用 Beautifulsoup 提取网页中所有 url 中的公司名称和其他信息
【发布时间】:2020-10-22 06:27:17
【问题描述】:
<li>
    <strong>Company Name</strong> 
    ":" 
    <span itemprop="name">PT ERA MURNI BUSANA</span>
</li>

在上面的 HTML 代码中,我试图提取公司名称,即 PT ERA MURNI BUSANA。 如果我使用单个测试链接,我可以使用我编写的单行代码获取名称:

soup.find_all("span",attrs={"itemprop":"name"})[3].get_text()

但我想从单个网页中的所有此类页面中提取信息。 所以我写了 for 循环,但它是获取详细信息。我正在粘贴我一直在尝试的需要修改的代码部分。 代码:-

   for link in supplierlinks:     #links have been extracted and merged with the base url
       r=requests.get(link,headers=headers)
       soup=BeautifulSoup(r.content,'lxml')
       companyname=soup.find_all("span",attrs={"itemprop":"name"})[2].get_text()

输出如下:

{'公司名称': 'AIRINDO SAKTI GARMENT PT'}

{'公司名称': '服装'}

{'公司名称': '服装'}

我需要的是公司名称,而不是输出中弹出的服装。如何修改for循环中的代码?

链接:https://idn.bizdirlib.com/node/5290

【问题讨论】:

  • 代码看起来不错。您可能必须通过添加 print 语句来检查汤,并检查它是否具有相同的名称和位置作为响应。
  • @Afrodille 请查看我的解决方案。

标签: python html beautifulsoup


【解决方案1】:

试试这个代码:

import requests
from bs4 import BeautifulSoup

headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0'}

r = requests.get('https://idn.bizdirlib.com/node/5290',headers=headers).text

soup = BeautifulSoup(r,'html5lib')

print(soup.find_all("span",attrs={"itemprop":"name"})[-1].get_text())

div = soup.find('div',class_ = "content clearfix")

li_tags = div.div.find_all('fieldset')[1].find_all('div')[-1].ul.find_all('li')

supplierlinks = []

for li in li_tags:
    try:
        supplierlinks.append("https://idn.bizdirlib.com/"+li.a['href'])
    except:
        pass

for link in supplierlinks:
    r = requests.get(link,headers=headers).text
    soup = BeautifulSoup(r,'html5lib')
    print(soup.find_all("span", attrs={"itemprop": "name"})[-1].get_text())

输出:

PT ERA MURNI BUSANA
PT ELKA SURYA ABADI
PT EMPANG BESAR MAKMUR
PT EMS
PT ENERON
PT ENPE JAYA
PT ERIDANI TOUR AND TRAVEL
PT EURO ASIA TRADE & INDUSTRY
PT EUROKARS CHRISDECO UTAMA
PT EVERAGE VALVES METAL
PT EVICO

此代码打印页面上所有链接的公司名称

【讨论】:

  • 非常感谢。有用。提取联系人时遇到一个问题。它显示列表索引超出范围。代码:-soup.find_all("span", attrs={"itemprop": "contactPoint"})[-1].get_text() 。对于单个测试链接,它虽然有效,但不在循环内。
  • 哦...会调查的。顺便说一句,如果我的回答对您有所帮助,请确保您接受我的回答作为最佳答案,方法是点击支持按钮下方的绿色刻度线。谢谢!
【解决方案2】:

您可以选择包含文本"Company Name" 的元素&lt;strong&gt; 的兄弟元素(另外,不要忘记设置User-Agent http 标头):

import requests 
from bs4 import BeautifulSoup


url = 'https://idn.bizdirlib.com/node/5290'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

print( soup.select_one('strong:contains("Company Name") + *').text )

打印:

PT ERA MURNI BUSANA

编辑:获取联系人:

import requests 
from bs4 import BeautifulSoup


url = 'https://idn.bizdirlib.com/node/5290'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

print( soup.select_one('strong:contains("Company Name") + *').text )
print( soup.select_one('strong:contains("Contact") + *').text )

打印:

PT ERA MURNI BUSANA
Mr.  Yohan  Kustanto

【讨论】:

  • 你能解释一下这部分代码:“'strong:contains("Company Name") + *'”吗?我不知道 select_one 方法。 “+ *”具体是什么意思?
  • @Afrodille 它将选择第一个兄弟姐妹来标记&lt;strong&gt;。标签&lt;strong&gt; 必须包含文本“公司名称”。这是 CSS 选择器:w3schools.com/cssref/css_selectors.asp
  • 非常感谢。最后的“+*”怎么样?
  • @Afrodille + 将选择紧跟在某个元素之后的元素。例如div + p 选择所有紧跟在&lt;div&gt; 元素之后的&lt;p&gt; 元素
  • 当我尝试使用以下代码行提取链接(上述问题)中提到的联系人时:contact_person=soup.select_one('strong:contains("Contact")+ *')。 text 它抛出一个错误:AttributeError: 'NoneType' object has no attribute 'text' .
猜你喜欢
  • 2021-04-08
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多