所以我是新來的Python(我來自PHP/JavaScript的背景),但我只是想寫一個快速的腳本爬行網站和所有子頁面找到所有a
標籤href
屬性,統計有多少,然後點擊鏈接。我可以統計所有的鏈接,但我不知道如何「點擊」鏈接,然後返回響應代碼。點擊鏈接與Python BeautifulSoup
from bs4 import BeautifulSoup
import urllib2
import re
def getLinks(url):
html_page = urllib2.urlopen(url)
soup = BeautifulSoup(html_page, "html.parser")
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
links.append(link.get('href'))
return links
anchors = getLinks("http://madisonmemorial.org/")
# Click on links and return responses
countMe = len(anchors)
for anchor in anchors:
i = getLinks(anchor)
countMe += len(i)
# Click on links and return responses
print countMe
這是甚至可能與BeautifulSoup
?
此外,我不是在尋找確切的代碼,我真正尋找的就像一個正確的方向使用函數調用或類似的東西。謝謝!
我認爲你不能用bs4執行點擊動作,也許看看硒?否則你可以在新鏈接中使用'urllib2.urlopen'? – PRMoureu
如果你想單擊它們只是爲了得到響應代碼,你可以使用'urllib2.urlopen'手中的url –