試圖湊這個網站:http://data.eastmoney.com/xg/xg/如何用刮不變的URL多頁 - 我蟒蛇
到目前爲止,我已經使用硒執行JavaScript,並獲得表刮掉。不過,我現在的代碼只能讓我成爲第一頁。我想知道是否有一種方法可以訪問其他17頁,因爲當我點擊下一頁時,URL不會改變,所以我不能每次迭代不同的URL
下面是我的代碼到目前爲止:
from selenium import webdriver
import lxml
from bs4 import BeautifulSoup
import time
def scrape():
url = 'http://data.eastmoney.com/xg/xg/'
d={}
f = open('east.txt','a')
driver = webdriver.PhantomJS()
driver.get(url)
lst = [x for x in range(0,25)]
htmlsource = driver.page_source
bs = BeautifulSoup(htmlsource)
heading = bs.find_all('thead')[0]
hlist = []
for header in heading.find_all('tr'):
head = header.find_all('th')
for i in lst:
if i!=2:
hlist.append(head[i].get_text().strip())
h = '|'.join(hlist)
print h
table = bs.find_all('tbody')[0]
for row in table.find_all('tr'):
cells = row.find_all('td')
d[cells[0].get_text()]=[y.get_text() for y in cells]
for key in d:
ret=[]
for i in lst:
if i != 2:
ret.append(d.get(key)[i])
s = '|'.join(ret)
print s
if __name__ == "__main__":
scrape()
或者是有可能對我來說,通過瀏覽器點擊旁邊如果我使用webdriver.Chrome(),而不是PhantomJS然後Python的新網頁上運行後,我點擊每一次?
我以爲我發現了一個重複,但無法找到它了。基本上應該有一種方法來調用一個onclick事件,然後調用一個方法來獲得DOM的新解釋。這是必要的,因爲如果URL沒有改變,這可能是因爲它會隨着某種AJAX調用而改變,需要你在使用美麗的湯之前與驅動程序一起操作。 –