【问题标题】:Scraping table in beautifulsoupBeautifulsoup 中的刮痧台
【发布时间】:2022-08-20 19:13:42
【问题描述】:

我发现了一个对我来说很难抓取的网页,我不确定为什么。它的表格数据在第一列带有超链接,地址和网址。我要做的是从第一页刮掉名称和位置,然后进入链接,刮掉电话号码并将其附加到我的另一个列表中。

理想情况下,CSV 应该有:标题、位置、省份、链接、电话。

我已经尝试了好几天,但我在兜圈子。请帮忙!

我的代码如下

import requests
from bs4 import BeautifulSoup
import pandas as pd
baseurl = [\"https://www.informa.es/directorio-empresas/0111_CULTIVO-CEREALES-EXCEPTO-ARROZ-LEGUMINOSAS-SEMILLAS-OLEAGINOSAS/Comunidad_CASTILLA-MANCHA.html#empresa\"]
urls = [f\'https://www.informa.es/directorio-empresas/0111_CULTIVO-CEREALES-EXCEPTO-ARROZ-LEGUMINOSAS-SEMILLAS-OLEAGINOSAS/Comunidad_CASTILLA-MANCHA/Empresas-{i}.html#empresa\'.format(i) for i in range(2,38)]


allurls = baseurl + urls
#print(allurls)
data = []
for url in allurls:
    page = requests.get(url)
    soup = BeautifulSoup(page.content, \"html.parser\")
    lists = soup.select(\"div#empresas_directorio ul\")

    #scrape the pages
    for lis in lists:
        title = lis.find(\'li\', class_=\"nom_empresa\").text
        location = lis.find(\'span\', class_=\"addressLocality\").text
        province = lis.find(\'span\', class_=\"addressRegion\").text
        link = lis.select_one(\"li.col1 a\")[\'href\']
        info = [title, location, province, link]
        #print(info)

        sub_page = requests.get(link)
        soup2 = BeautifulSoup(sub_page.content, \"html.parser\")
        phone=soup2.select_one(\'tel\')
        telephone = phone.text if phone else None
        #print([title,location,province,link,telephone])
        data.append([title, location, province, link, telephone])


cols = [\"title\", \"location\", \"province\", \"link\", \"telephone\"]

df = pd.DataFrame(data, columns=cols)
print(df)
df.to_csv(\'CSM.csv\',index = False)

    标签: python html dataframe web-scraping beautifulsoup


    【解决方案1】:

    怎么了?

    我不确定您为什么在第一部分中选择<ul> 和<li> 而数据放在表格中 - 这样它就不起作用了。在第二部分中,您尝试通过选择标签tel 来获取电话号码,但只有一个带有此名称的class。

    怎么修?

    我不会为您详细解决所有问题,只是展示如何使严重错误运行。

    首先选择保存数据的表的行:

    soup.select('[itemprop="itemListElement"]')
    

    在遍历结果集时,只需通过其 item 属性选择所需的元素:

    title = lis.select_one('[itemprop="name"]').text
    location = lis.select_one('[itemprop="addressLocality"]').text
    province = lis.select_one('[itemprop="addressRegion"]').text
    link = lis.select_one('a')['href']
    

    按类别从附加请求中选择电话号码:

    soup2.select_one('.tel')
    

    重点示例

    ...
    
    lists = soup.select('[itemprop="itemListElement"]')
    
    #scrape the pages
    for lis in lists:
        title = lis.select_one('[itemprop="name"]').text
        location = lis.select_one('[itemprop="addressLocality"]').text
        province = lis.select_one('[itemprop="addressRegion"]').text
        link = lis.select_one('a')['href']
        info = [title, location, province, link]
       
        sub_page = requests.get(link)
        soup2 = BeautifulSoup(sub_page.content, "html.parser")
        phone=soup2.select_one('.tel')
        telephone = phone.text if phone else None
        data.append([title, location, province, link, telephone])
    ...
    

    输出

        title   location    province    link    telephone
    0   BASCULA AGRARIA SOCIEDAD LIMITADA.  Membrilla   CIUDAD REAL https://www.informa.es/directorio-empresas/Emp...   691262748
    1   MARCUNEZ INVERMOL SL    Castillo de Garcimuñoz  CUENCA  https://www.informa.es/directorio-empresas/Emp...   None
    2   ALJOFRUIT VERDURAS SOCIEDAD LIMITADA.   Picazo (El) CUENCA  https://www.informa.es/directorio-empresas/Emp...   None
    ...
    

    【讨论】:

    • 所以我已经完成并调整了所有内容,但它仍然返回空的df。我不应该选择整个表格而不是“itemListElement”吗?我已经尝试过这些变体,但它从不接受任何值。
    • @NickGordon:建议,一步一步来 - 1. 看看你的汤 - 你是否获得了你需要的信息或一些带有机器人检测/阻止的消息/...... 2. 不要遍历所有页面 - 只需尝试一个然后是其中两个,因为它需要时间来执行,并且您正在不必要地渗透该网站。 3...
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 2016-01-25
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多