我如何才能最好地隔离2个不同的未标记的html片段使用美丽的汤打印到CSV?

2024-04-19 10:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

作为序言,我是一个python初学者,这是我第一次使用BeautifulSoup。任何意见都将不胜感激。你知道吗

我正试图从this site中搜集所有公司名称和电子邮件地址。有3层链接可以浏览(按字母顺序排列的分页列表->;按字母排列的公司列表->;公司详细信息页),然后我会将它们打印到csv。你知道吗

到目前为止,我已经能够用下面的代码隔离按字母顺序排列的链接列表,但是在尝试隔离不同的公司页面,然后从未标记的html中提取姓名/电子邮件时,我遇到了麻烦。你知道吗

import re
import urllib2
from bs4 import BeautifulSoup

page = urllib2.urlopen('http://www.indiainfoline.com/Markets/Company/A.aspx').read()
soup = BeautifulSoup(page)
soup.prettify()

pattern = re.compile(r'^\/Markets\/Company\/\D\.aspx$')

all_links = []
navigation_links = []
root = "http://www.indiainfoline.com/"

# Finding all links
for anchor in soup.findAll('a', href=True):
    all_links.append(anchor['href'])
# Isolate links matching regex
for link in all_links:
    if re.match(pattern, link):
        navigation_links.append(root + re.match(pattern, link).group(0))
navigation_links = list(set(navigation_links))

company_pages = []
for page in navigation_links:
    for anchor in soup.findAll('table', id='AlphaQuotes1_Rep_quote')              [0].findAll('a',href=True):
        company_pages.append(root + anchor['href'])

Tags: inimportre列表for字母page公司
1条回答
网友
1楼 · 发布于 2024-04-19 10:22:19

一件件地。获取每个公司的链接很容易:

from bs4 import BeautifulSoup
import requests

html = requests.get('http://www.indiainfoline.com/Markets/Company/A.aspx').text
bs = BeautifulSoup(html)

# find the links to companies
company_menu = bs.find("div",{'style':'padding-left:5px'})
# print all companies links
companies = company_menu.find_all('a')
for company in companies:
    print company['href']

其次,获取公司名称:

for company in companies:
    print company.getText().strip()

第三,电子邮件有点复杂,但您可以在这里使用regex,因此在独立的公司页面中,请执行以下操作:

import re
# example company page
html = requests.get('http://www.indiainfoline.com/Markets/Company/Adani-Power-Ltd/533096').text
EMAIL_REGEX = re.compile("mailto:([A-Za-z0-9.\-+]+@[A-Za-z0-9_\-]+[.][a-zA-Z]{2,4})")
re.findall(EMAIL_REGEX, html)
# and there you got a list of found emails
...

干杯

相关问题 更多 >