1
我寫了一個小腳本,用python從網頁中讀取所有的hrefs。 但它有一個問題。例如,它不會讀取href="pages.php?ef=fa&page=n_fullstory.php&NewsIDn=1648"
。網絡抓取:全部href
代碼:
import urllib
import re
urls = ["http://something.com"]
regex='href=\"(.+?)\"'
pattern = re.compile(regex)
htmlfile = urllib.urlopen(urls[0])
htmltext = htmlfile.read()
hrefs = re.findall(pattern,htmltext)
print hrefs
任何人可以幫助我嗎?謝謝。
一般建議:不要用正則表達式解析HTML。雖然你可以實施你的特定案例,但如果你需要更多的東西,它可能會非常快速地變得非常混亂。改爲使用正確的解析庫。查看[BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/)或['lxml.html'](http://lxml.de/lxmlhtml.html)。或者甚至可能是[Scrapy](https://scrapy.org/)。 – drdaeman