我正在嘗試搜刮這個網站,這是我目前的代碼:
import click
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
table_rows = []
url = 'https://www.iagco.agco.ca/prod/pub/en/Default.aspx?PossePresentation=PublicNoticeSearch'/span>
driver = webdriver.Chrome('/Applications/Python 3.9/chromedriver')
driver.get(url)
driver.find_element_by_xpath("/html/body/div[1]/form/div[3]/div[2]/div/div/div[5]/div/table/tbody/tr[5]/td[3]/span/select/option[2]").click()
driver.implicitly_wait(1)
driver.find_element_by_xpath("/html/body/div[1]/form/div[3]/div[2]/div/div/div[6]/div/table/tbody/tr/td/div/a").點擊()
driver.implicitly_wait(1)
soup = BeautifulSoup(driver.page_source, 'lxml')
tables = soup.find_all('table')
driver.implicitly_wait(2)
for table in driver.find_elements_by_xpath('/html/body/div[1]/form/div[3]/div[2]/div/div[5]/div/table/tbody/tr/td')。
data = [item.text for item in table.find_elements_by_xpath('/html/body/div[1]/form/div[3]/div[2]/div/div/div/td')]
print(data)
列印出來的是一個長長的串列,我想知道如何把它變成一個表格格式。
['City Premises Deadline for Objections / Submissions File Number Application Type Areas
BRAMPTON Tweed
10010 Mclaughlin Rd N
BRAMPTON, ON L7A2X6 2021-09-24 1248893 新申請
大麻零售店授權室內區域 檔案反對/提交
布蘭頓-特威德(BRAMPTON Tweed
1990 Steeles Ave W
BRAMPTON, ON L6Y0R4 2021-09-24 1250690 新申請
大麻零售店授權 室內區域 反對/提交檔案
CAMPBELLVILLE Welcome Cannabis Campbellville
6 MAIN ST N
CAMPBELLVILLE, ON L0P1B0 2021-09-29 1272273 新申請
大麻零售店授權 室內區域 檔案反對/提交
坎寧頓Bud Runners大麻公司
17 Cameron ST W.
CANNINGTON, ON L0E 1E0 2021-09-23 1271708 新申請
大麻零售店授權 室內區域 檔案異議/提交
ETOBICOKE Cannabis 151
188 the queensway
ETOBICOKE, ON M8Y1J3 2021-09-20 1157846 新申請
大麻零售店授權 室內區域 反對/提交檔案
ETOBICOKE Fire & Flower Cannabis Co.
764 the queensway
ETOBICOKE, ON M8Z0E8 2021-09-30 1211417 新申請
大麻零售店授權書 室內區域 反對/提交檔案
格爾芙-特威德
138 college ave w unit a
GUELPH, ON N1G1S4 2021-09-28 1267278 新申請
大麻零售店授權室內區域 檔案反對/提交
Hamilton harvest cannabis co
318 queenston rd unit g
HAMILTON, ON L8K1H5 2021-09-23 1283383 新申請
大麻零售店授權室內區域 檔案反對/提交
哈密頓Lady Leaf
372 King St E Suite 101
HAMILTON, ON L8N1C3 2021-09-25 1188839 新申請
大麻零售店授權 室內區域 反對/提交檔案
HAVELOCK 陽光魚大麻培訓基地
30 ontario street, east unit
HAVELOCK, ON K0L1Z0 2021-10-02 1285465 新申請
大麻零售店授權書 室內區域檔案 反對/提交
廚子的大麻店-國王E
325 KING ST E
KITCHENER, ON N2G2L2 2021-10-04 1297162 新申請
大麻零售店授權 室內區域 檔案反對/提交
莫里斯堡市 莫里斯堡市大麻協會
主街137號
MORRISBURG, ON K0C1X0 2021-10-01 1196780 新申請
大麻零售店授權 室內區域 檔案異議/提交
MORRISBURG, SOUTH DUNDAS The Oz Store
147 main st., unit #2
MORRISBURG, SOUTH DUNDAS, ON K0C 1X0 2021-09-30 1190679 新申請
大麻零售店授權書 室內區域 反對/提交檔案
MORRISTON Welcome Cannabis
3 BADENOCH ST
MORRISTON, ON N0B 2C0 2021-10-01 1256018 新申請
大麻零售店授權 室內區域 反對/提交檔案
北約:"味蕾 "大麻公司
勞倫斯大街西段1193號
NORTH YORK, ON M6A1E2 2021-09-25 1266788 新申請
大麻零售店授權 室內區域 反對/提交檔案
加德滿都藍鳥大麻公司
27 YORK ST
OTTAWA, ON K1N5S7 2021-10-03 1221838 新申請
大麻零售店授權的室內區域 檔案反對/提交
加德滿都行星地球大麻公司
1666銀行街600號套房
OTTAWA, ON K1V7Y6 2021-09-27 1215970 新申請
大麻零售店授權 室內區域 檔案反對/提交
Stittsville Shinybud cannabis co. STITTSVILLE
1261 Main St Unit 2
STITTSVILLE, ON K2S2E4 2021-10-01 1167849 新申請
大麻零售店授權 室內區域 反對/提交檔案
多倫多黑星大麻商店
985 dovercourt rd
TORONTO, ON M6H2X6 2021-09-30 1277446 新申請
大麻零售店授權 室內區域 反對/提交檔案
多倫多Canna North Cannabis Store
117 YONGE ST
TORONTO, ON M5C1W4 2021-10-02 1001902 新申請
大麻零售店授權 室內區域 反對/提交檔案
多倫多橄欖罐
554 ANNETTE ST
TORONTO, ON M6S2C2 2021-10-02 1196618 新申請
大麻零售店授權 室內區域 檔案反對/提交
WATERLOO The Cannabist Shop - Bridgeport W
10 BRIDGEPORT RD W
WATERLOO, ON N2L2Y1 2021-10-04 1294285 新申請
大麻零售店授權 室內區域 反對/提交檔案
惠靈頓社區商店
186 Main Street Unit 3
WELLINGTON, ON K0K 3L0 2021-09-23 1252887 新申請
大麻零售店授權室內區域 檔案反對/提交
溫徹斯特藍鳥大麻公司-溫徹斯特
507 St Lawrence St lower level
WINCHESTER, ON K0C2K0 2021-10-01 1299098 新申請
大麻零售店授權室內區域檔案反對/提交'] 。
uj5u.com熱心網友回復:
我得到了以下表格格式的輸出。
代碼:
import click
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import pandas as pd
資料 = []
driver = webdriver.Chrome('chromedriver.exe')
driver.maximum_window()
url = 'https://www.iagco.agco.ca/prod/pub/en/Default.aspx?PossePresentation=PublicNoticeSearch'/span>
driver.get(url)
time.sleep(8)
driver.find_element_by_xpath(
"/html/body/div[1]/form/div[3]/div[2]/div/div/div[5]/div/table/tbody/tr[5]/td[3]/span/select/option[2]").click()
driver.implicitly_wait(1)
driver.find_element_by_xpath(
"/html/body/div[1]/form/div[3]/div[2]/div/div[6]/div/table/tbody/tr/td/div/a").click()
driver.implicitly_wait(1)
soup = BeautifulSoup(driver.page_source, 'lxml')
t = soup.find('table', class_='possegrid')
# 從表中獲取所有的行。
trs = t.select('tr')
for tr in trs:
data.append(tr. stripped_strings)
df = pd.DataFrame(data)
print(df)
輸出:
0 City Premises ... 室內區域檔案異議/提交
1 City Premises ... 無 無 無
2 BRAMPTON Tweed ... 無 無 2 BRAMPTON Tweed ...
3 BRAMPTON Tweed ... 無 無 3 BRAMPTON Tweed ...
4 CAMPBELLVILLE Welcome Cannabis Campbellville ... None None5 CANNINGTON Bud Runners Cannabis ... None None None
6 ETOBICOKE Cannabis 151 ... None None ...
7 ETOBICOKE Fire & Flower Cannabis Co. ... 無 無 無 ...
8 GUELPH Tweed ... None None
9 HAMILTON HARVEST CANNABIS CO ... 無 無 無
10 HAMILTON Lady Leaf ... None None11 HAVELOCK Sunfish Cannabis Trainstation ... None None
12 KITCHENER THE CANNABIST SHOP - KING E ... None None
13 MORRISBURG Morrisburg cannabis ... 無 無 無
14 MORRISBURG, SOUTH DUNDAS The Oz Store ... None None
15 MORRISTON Welcome Cannabis ... None None
16 NORTH YORK Taste Buds Cannabis ... None None
17 OTTAWA BlueBird Cannabis Co ... 無 無 無
18 OTTAWA Planet Earth Cannabis ... None None
19 STITTSVILLE SHINYBUD CANNABIS CO. Stittsville ... 無 無 無
20 TORONTO BLACKSTAR CANNABIS SHUTER ... 無 無 無
21 TORONTO Canna North Cannabis Store ... None None None
22 TORONTO橄欖罐 ... 無 無 22 TORONTO Olive ...
23 WATERLOO The Cannabist Shop - Bridgeport W ... None None None
24 WELLINGTON The Community Store ... None None
25 WINCHESTER BlueBird Cannabis Co - Winchester ... None None None
[26 行 x 246 列]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/327067.html
標籤:
