我正在嘗試從靜態網頁中抓取所有檔案名及其相關地址。我已經創建的腳本幾乎可以準確地獲取它們,除了在某些地址中的某個位置保留一個空格。為了更清楚,除了其他結果之外的腳本在控制臺中列印以下內容:
RZ000089 1207, 1211, 1215, 1217, 1219 & 1221Carlisle Avenue
而我的預期輸出是(注意前面的空格Carlisle Avenue):
RZ000089 1207, 1211, 1215, 1217, 1219 & 1221 Carlisle Avenue
目前的做法:
import requests
from bs4 import BeautifulSoup
link = 'https://www.esquimalt.ca/business-development/development-tracker/rezoning-applications'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}
with requests.Session() as s:
s.headers.update(headers)
res = s.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("table.table_two_columns > tbody"):
file = item.select_one("tr > td:has(strong:-soup-contains('File:'))").get_text(strip=True).replace("File:","").replace(" "," ").strip()
addr_list = [i.text for i in item.select("tr:nth-of-type(1) > td:nth-of-type(1) > p")]
for addr in addr_list:
print(file,addr)
我得到的輸出(截斷):
RZ000095 A-904 Admirals Road
RZ000089 1207, 1211, 1215, 1217, 1219 & 1221Carlisle Avenue
RZ000089 512 & 522 Fraser Street
RZ000089 1212, 1216, 1220, 1222, 1224 & 1226Lyall Street
RZ000055 1072 Colville Road
RZ000056 1076 Colville Road
我希望得到的輸出(注意 和 之前的空格Carlisle Avenue)Lyall Street:
RZ000095 A-904 Admirals Road
RZ000089 1207, 1211, 1215, 1217, 1219 & 1221 Carlisle Avenue
RZ000089 512 & 522 Fraser Street
RZ000089 1212, 1216, 1220, 1222, 1224 & 1226 Lyall Street
RZ000055 1072 Colville Road
RZ000056 1076 Colville Road
uj5u.com熱心網友回復:
而不是i.text使用引數:i.get_text()separator=
import requests
from bs4 import BeautifulSoup
link = "https://www.esquimalt.ca/business-development/development-tracker/rezoning-applications"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}
with requests.Session() as s:
s.headers.update(headers)
res = s.get(link)
soup = BeautifulSoup(res.text, "lxml")
for item in soup.select("table.table_two_columns > tbody"):
file = (
item.select_one("tr > td:has(strong:-soup-contains('File:'))")
.get_text(strip=True)
.replace("File:", "")
.replace(" ", " ")
.strip()
)
addr_list = [
i.get_text(strip=True, separator=" ")
for i in item.select("tr:nth-of-type(1) > td:nth-of-type(1) > p")
]
for addr in addr_list:
print(file, addr)
印刷:
RZ000095 A-904 Admirals Road
RZ000089 1207, 1211, 1215, 1217, 1219 & 1221 Carlisle Avenue
RZ000089 512 & 522 Fraser Street
RZ000089 1212, 1216, 1220, 1222, 1224 & 1226 Lyall Street
RZ000055 1072 Colville Road
RZ000056 1076 Colville Road
RZ000098 812 Craigflower Road
RZ000083 881 Craigflower Road
RZ000071 820 Dunsmuir Road
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/467198.html
標籤:Python python-3.x 网页抓取 美丽的汤 蟒蛇请求
下一篇:字典差異類似于集合差異
