网页抓取:阅读所有 href
Web scraping: read all href
我写了一个小脚本来使用 python 从网页中读取所有 href。
但它有一个问题。例如,它不会读取 href="pages.php?ef=fa&page=n_fullstory.php&NewsIDn=1648"
。
代码:
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
有人可以帮助我吗?谢谢
使用BEautifulSoup并请求静态网站。这是一个很棒的网络抓取模块,使用代码,您可以轻松地获取 href 标签内的值。希望对你有帮助
import requests
from bs4 import BeautifulSoup
url = 'whatever url you want to parse'
result = requests.get(url)
soup = BeautifulSoup(result.content,'html.parser')
for a in soup.find_all('a',href=True):
print "Found the URL:", a['href']
我写了一个小脚本来使用 python 从网页中读取所有 href。
但它有一个问题。例如,它不会读取 href="pages.php?ef=fa&page=n_fullstory.php&NewsIDn=1648"
。
代码:
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
有人可以帮助我吗?谢谢
使用BEautifulSoup并请求静态网站。这是一个很棒的网络抓取模块,使用代码,您可以轻松地获取 href 标签内的值。希望对你有帮助
import requests
from bs4 import BeautifulSoup
url = 'whatever url you want to parse'
result = requests.get(url)
soup = BeautifulSoup(result.content,'html.parser')
for a in soup.find_all('a',href=True):
print "Found the URL:", a['href']