【问题标题】:How to save value in excel from site using python?如何使用python从网站保存excel中的价值?
【发布时间】:2021-05-22 16:01:38
【问题描述】:

下面是我想抓取网站并将值存储在 excel 中的代码-

  **

import pandas as pd
        import requests
        from bs4 import BeautifulSoup
        from openpyxl import workbook
        
        Name = []
        Mob = []
        Add = []
        E_mail = []
        website = []
        
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
        }
        
        url = requests.get("www.example.com", headers=headers)
        
        soup = BeautifulSoup(url.content, 'html.parser')
        travel_name = soup.findAll(attrs={'class': 'list-group-item'})
        
        
        for name in travel_name:
            for a in name.findAll('a', attrs={"class": "text-warning"}):
                user = a.text
                Name.append(user)
            pList = name.findAll('p', attrs={"class": "mb-2 text-truncate"})
            for p in pList:
                #   print(p.text)
                if p.text.find("Contact:") != -1:
                    contact = str.replace(p.text, "Contact:", "")
                    Mob.append(contact)
                #    print(contact)
                if p.text.find("Location:") != -1:
                    location = str.replace(p.text, "Location:", "")
                    Add.append(location)
                #    print(location)
                if p.text.find("Email:") != -1:
                    email = str.replace(p.text, "Email:", "")
                    E_mail.append(email)
                #   print(email)
                if p.text.find("Website:") != -1:
                    web = str.replace(p.text, "Website:", "")
                    website.append(web)

**

我想在 excel 中按列存储值。我试过 df = pd.DataFrame() 但我失败了 [名称、成员、地址、电子邮件、网站]

【问题讨论】:

    标签: python python-2.7 python-requests screen-scraping


    【解决方案1】:

    请遵循以下模式,希望您理解,如果不能随时要求更多解释:

        data=[]
        for name in travel_name:
            dict_={}   #i.e create a dict for each item i.e it will be representing a row in the excel
            name= [a.text for a in name.findAll('a', attrs={"class": "text-warning"})]
            contact=(code to extract the value like in name)
            email=(code to extract the value like in name)
            website=(code to extract the value like in name)
            data.append(dict) #i.e append each dictionary(later to be row in excel) into a list
        df=pd.DataFrame(data)
        df.to_csv('data',index=False)
    

    这里 name、contact、email、website 将是列的名称,每次迭代都会根据您的数据为这些列创建一行。

    【讨论】:

    • contact=(code to extract the value like in name) 请解释一下这行怎么写?
    • 是的,就像我为 name 变量编写的一样,您需要编写包含该信息的标签
    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 2020-05-28
    • 2020-07-26
    • 2021-08-10
    • 1970-01-01
    • 2017-05-02
    • 2018-01-14
    • 2022-01-08
    相关资源
    最近更新 更多