繁体   English   中英

从.csv 中读取 URL 并在前面使用 Python、BeautifulSoup、Z251D2BBFE9A3DC6954AZ6FE9A3DC698B4AZ3

[英]Reading URLs from .csv and appending scrape results below previous with Python, BeautifulSoup, Pandas

尽管有很多无知,我还是让这段代码几乎可以工作。 请帮助本垒打!

  • 问题1:输入:

我有一长串要读取的 URL (1000+),它们位于.csv 的单列中。 我宁愿从该文件中读取,也不愿将它们粘贴到代码中,如下所示。

  • 问题2:OUTPUT:

源文件实际上有 3 个驱动程序和 3 个挑战。 在一个单独的 python 文件中,下面的代码找到、打印并保存所有 3 个,但当我使用下面的 dataframe 时(见下文 - 它只保存 2 个)。

  • 问题3:OUTPUT:

我希望 output(两个文件)在第 0 列中具有 URL,然后在以下列中具有驱动程序(或挑战)。 但是我在这里写的(可能是“drop”)使它们不仅下降了一行,而且还移动了 2 列。

最后,我同时展示了输入和当前和所需的 output。 对不起,很长的问题。 我将非常感谢任何帮助!

import requests
from bs4 import BeautifulSoup
import pandas as pd

urls = ['https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Induction-Hobs-30196623/', 'https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Human-Capital-Management-30196628/', 'https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Probe-Card-30196643/']
dataframes = []
dataframes2 = []

for url in urls:
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')
    toc = soup.find("div", id="toc")

    def get_drivers():
        data = []
        for x in toc.select('li:-soup-contains-own("Market drivers") li'):
            data.append(x.get_text(strip=True))
        df = pd.DataFrame(data, columns=[url])
        dataframes.append(pd.DataFrame(df).drop(0, axis=0))
        df2 = pd.concat(dataframes)
        tdata = df2.T
        tdata.to_csv(f'detail-dr.csv', header=True)

    get_drivers()


    def get_challenges():
        data = []
        for y in toc.select('li:-soup-contains-own("Market challenges") li'):
            data.append(y.get_text(strip=True).replace('Table Impact of drivers and challenges', ''))
        df = pd.DataFrame(data, columns=[url])
        dataframes2.append(pd.DataFrame(df).drop(0, axis=0))
        df2 = pd.concat(dataframes2)
        tdata = df2.T
        tdata.to_csv(f'detail-ch.csv', header=True)

    get_challenges()

每个 URL 中的输入如下所示。 它们只是列表:

市场驱动力

  • 对晶圆厂的投资不断增长
  • 电子产品小型化
  • 对物联网设备的需求不断增长

市场挑战

  • 半导体行业的快速技术变革
  • 半导体行业的波动
  • 技术鸿沟的影响 表 驱动因素和挑战的影响

我想要的驱动程序 output 是:

0 1 2 3
http/.../Global-Induction-Hobs-30196623/ 产品创新和新设计 随着生活方式的改变,对便利家电的需求不断增加 越来越多地采用节能电器
http/.../Global-Human-Capital-Management-30196628/ 对自动化招聘流程的需求 对所有 HR 职能统一解决方案的需求不断增长 增加劳动力多样性
http/.../Global-Probe-Card-30196643/ 对晶圆厂的投资不断增长 电子产品小型化 对物联网设备的需求不断增长

但相反,我得到:

0 1 2 3 4 5 6
http/.../Global-Induction-Hobs-30196623/ 随着生活方式的改变,对便利家电的需求不断增加 越来越多地采用节能电器
http/.../Global-Human-Capital-Management-30196628/ 对所有 HR 职能统一解决方案的需求不断增长 增加劳动力多样性
http/.../Global-Probe-Card-30196643/ 电子产品小型化 对物联网设备的需求不断增长

将您的数据存储在字典列表中,从中创建一个数据框。 drivers / challenges列表拆分为columns ,并将其连接到最终数据帧。

例子

import requests
from bs4 import BeautifulSoup
import pandas as pd

urls = ['https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Induction-Hobs-30196623/', 'https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Human-Capital-Management-30196628/', 'https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Probe-Card-30196643/']
data = []

for url in urls:
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')
    toc = soup.find("div", id="toc")

    def get_drivers():
        data.append({
            'url':url,
            'type':'driver',
            'list':[x.get_text(strip=True) for x in toc.select('li:-soup-contains-own("Market drivers") li')]
        })

    get_drivers()


    def get_challenges():
        data.append({
            'url':url,
            'type':'challenges',
            'list':[x.get_text(strip=True) for x in toc.select('li:-soup-contains-own("Market challenges") li') if x.get_text(strip=True) != 'Table Impact of drivers and challenges' ]
        })

    get_challenges()

    
pd.concat([pd.DataFrame(data)[['url','type']], pd.DataFrame(pd.DataFrame(data).list.tolist())],axis = 1)

Output

url 类型 0 1 2
https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Induction-Hobs-30196623/ 司机 产品创新和新设计 随着生活方式的改变,对便利家电的需求不断增加 越来越多地采用节能电器
https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Induction-Hobs-30196623/ 挑战 高成本限制了大众市场的采用 与电磁炉相关的健康危害 仅使用平面器具和感应专用炊具的限制表 驱动因素和挑战的影响
https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Human-Capital-Management-30196628/ 司机 对自动化招聘流程的需求 对所有 HR 职能统一解决方案的需求不断增长 增加劳动力多样性
https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Human-Capital-Management-30196628/ 挑战 来自开源软件的威胁 实施和维护成本高 对数据安全的威胁表驱动因素和挑战的影响
https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Probe-Card-30196643/ 司机 对晶圆厂的投资不断增长 电子产品小型化 对物联网设备的需求不断增长
https://www.marketresearch.com/Infiniti-Research-Limited-v2680/Global-Probe-Card-30196643/ 挑战 半导体行业的快速技术变革 半导体行业的波动 技术鸿沟的影响表驱动因素和挑战的影响

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM