如何使用 python 从站点保存 excel 中的值?
How to save value in excel from site using python?
下面是我的代码 我想抓取网站并在 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() 但我失败了
[姓名、人物、地址、E_mail、网站]
按照下面的模式希望你能理解,如果不方便要求更多解释:
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 将是列的名称,每次迭代都会根据您的数据为这些列创建一行。
下面是我的代码 我想抓取网站并在 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() 但我失败了 [姓名、人物、地址、E_mail、网站]
按照下面的模式希望你能理解,如果不方便要求更多解释:
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 将是列的名称,每次迭代都会根据您的数据为这些列创建一行。