【问题标题】:Is there any way to select specific a tags using beautiful soup?有没有办法使用漂亮的汤来选择特定的标签?
【发布时间】:2020-09-24 16:48:58
【问题描述】:

我正在使用 BeautifulSoup 并尝试打印所有仅包含公司网站 url 的标签 href。但我的代码也在选择其他href。总共有 71 个公司的网站链接,但我的代码没有选择所有这些 href。 这是我从中提取数据的source

这是我的代码


import requests
import pandas as pd
from bs4 import BeautifulSoup

url = 'https://www.constructionplacements.com/top-construction-companies-in-india-2020/'

name_data = []
website_data = []

print(url)

soup = BeautifulSoup(requests.get(url).content, 'html.parser')

# Loop to select and print all companies title
for h in soup.select('h4'):
    print(h.text)
    name_data.append(h.text)

# Loop to select and print all companies website url
for w in soup.select('p em a'):
    print(w['href'])
    website_data.append(w['href'])

df = pd.DataFrame({
    'Company Title': name_data,
    'Website': website_data
})
print(df)

df.to_csv('ata.csv')

【问题讨论】:

  • 这和 Pandas 有什么关系?请仅包含相关标签。
  • @noah 我正在使用 panda 将其保存为 csv 格式
  • 请将您的问题表述为MRE。如果您需要编辑以添加包含 Pandas 的代码,那么它必须与您的问题无关。添加 Pandas 信息只会让您更难获得答案,因为这个问题现在有额外的、不必要的复杂性。
  • @noah 你是对的。谢谢你,下次我会确保使用更多相关的标签:-)

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


【解决方案1】:

要获取公司的所有链接,您可以使用以下示例:

import re
import requests 
from bs4 import BeautifulSoup


url = 'https://www.constructionplacements.com/top-construction-companies-in-india-2020/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

for h4 in soup.find_all(lambda t: t.name=='h4' and re.search(r'^\d+\s*\.', t.text)):
    print('{:<75} {}'.format(h4.text, h4.find_next('a')['href']))

打印:

1. L&T Engineering & Construction Division (L&T ECC), Chennai               http://www.lntecc.com/
2. Tata Projects Ltd, Mumbai                                                http://www.tataprojects.com/
3. Shapoorji Pallonji & Co Ltd, Mumbai                                      https://www.shapoorjipallonji.com/
4. GMR Group, Mumbai                                                        http://www.gmrgroup.in/
5. Hindustan Construction Company (HCC), Mumbai                             http://www.hccindia.com/
6. Afcons Infrastructure Limited, Mumbai                                    http://www.shapoorjipallonji.com/
7. JMC Projects, Mumbai                                                     https://www.jmcprojects.com/
8. Gammon India Ltd, Mumbai                                                 http://www.gammonindia.com
9. IVRCL, Hyderabad                                                         http://www.ivrcl.com/
10. J Kumar Infra, Mumbai                                                   http://www.jkumar.com/
11. Gammon Infrastructure Projects Limited (GIPL), Mumbai                   http://www.gammoninfra.com/
12. Reliance Infrastructure                                                 http://www.rinfra.com
13. Ashoka Buildcon, Nashik                                                 https://ashokabuildcon.com/
14. B L Kashyap & Sons Ltd (BLK), New Delhi                                 http://www.blkashyap.com
15. Consolidated Construction Consortium Ltd (CCCL), Chennai                http://www.ccclindia.com/
16. Essar Group, Mumbai                                                     https://www.essar.com/

...and so on.

【讨论】:

  • 谢谢!不知道我们也可以在 BeautifulSoup 中使用正则表达式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 2018-12-17
  • 2015-11-19
  • 1970-01-01
  • 2012-12-27
  • 2015-05-03
相关资源
最近更新 更多