import requests
from bs4 import BeautifulSoup
import pandas as pd
headers= {'User-Agent': 'Mozilla/5.0'}
#put all item in this array
response = requests.get('http://smartcatalog.emo-milano.com/it/espositore/a-mannesmann-maschinenfabrik-gmbh')
soup = BeautifulSoup(response.content, 'html.parser')
table=soup.find_all('table', class_='expo-table general-color')
for row in table:
for up in row.find_all('td'):
text_list = [text for text in up.stripped_strings]
print(text_list)
這些代碼運行良好,它們會給我正確的輸出,但它們不會以這些格式提供輸出,如下所示我想要這些格式的輸出,你能幫我嗎
Indirizzo Bliedinghauserstrasse 27
Città Remscheid
Nazionalità Germania
Sito web www.amannesmann.de
Stand Pad. 3 E14 F11
Telefono 492191989-0
Fax 492191989-201
E-mail [email protected]
Membro di Cecimo
Social
uj5u.com熱心網友回復:
pandas 有一個內置的 html table scraper,所以你可以運行:
df = pd.read_html('http://smartcatalog.emo-milano.com/it/espositore/a-mannesmann-maschinenfabrik-gmbh')
這將回傳頁面上所有表的串列作為資料框,您可以使用以下命令訪問您的資料df[0]:
| 0 | 1 | |
|---|---|---|
| 0 | 因迪里佐 | 布萊丁豪瑟大街 27 號 |
| 1 | 心 | 雷姆沙伊德 |
| 2 | 民族主義 | 日耳曼尼亞 |
| 3 | 網站 | www.amannesmann.de |
| 4 | 站立 | 軟墊。3 E14 F11 |
| 5 | 電話 | 492191989-0 |
| 6 | 傳真 | 492191989-201 |
| 7 | 電子郵件 | 銷售@mannesmann.de |
| 8 | 會員 | 南 |
| 9 | 社會的 | 南 |
uj5u.com熱心網友回復:
您可以使用.get_text()方法來提取文本并使用引數來避免空格并使用separator
data=table.find_all("tr")
for i in data:
print(i.get_text(strip=True,separator=" "))
輸出:
Indirizzo Bliedinghauserstrasse 27
Città Remscheid
...
uj5u.com熱心網友回復:
而不是選擇<td>,選擇<tr>并使用.stripped_strings它來獲取行明智的資料,然后將它們附加到資料框。
這是代碼
import requests
from bs4 import BeautifulSoup
import pandas as pd
headers= {'User-Agent': 'Mozilla/5.0'}
#put all item in this array
temp = []
response = requests.get('http://smartcatalog.emo-milano.com/it/espositore/a-mannesmann-maschinenfabrik-gmbh')
soup = BeautifulSoup(response.content, 'html.parser')
table=soup.find_all('table', class_='expo-table general-color')
for row in table:
for up in row.find_all('tr'):
temp.append([text for text in up.stripped_strings])
df = pd.DataFrame(temp)
print(df)
0 1
0 Indirizzo Bliedinghauserstrasse 27
1 Città Remscheid
2 Nazionalità Germania
3 Sito web www.amannesmann.de
4 Stand Pad. 3 E14 F11
5 Telefono 492191989-0
6 Fax 492191989-201
7 E-mail [email protected]
8 Membro di None
9 Social None
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/353868.html
上一篇:將pandas輸出更改為串列
