【发布时间】:2019-06-06 02:38:30
【问题描述】:
我正在尝试编写一个只处理网站内部链接的爬虫。我正在使用 python 2.7,漂亮的汤和请求,我需要所有内部链接(绝对和亲属)。
我的客户向我请求了一个网站爬虫,但我希望它只爬取内部链接。我需要它忽略 jpg/png/gif 和其他类型的 url,所以它只处理页面。
import re, request
from bs4 import BeautifulSoup
def processUrl(url):
if not url in checkedUrls:
try:
if 'text/html' in requests.head(url).headers['Content-Type']:
req=requests.get(url)
if req.status_code==200:
print url
checkedUrls.append(url)
html=BeautifulSoup(req.text,'html.parser')
pages=html.find_all('a')
for page in pages:
url=page.get('href')
processUrl(url)
except:
pass
checekdUrls=[]
url='http://sampleurl.com'
processUrl(url)
【问题讨论】:
-
你只需要在开始爬取之前添加一个额外的逻辑来检查域是否相同。如果不只是返回给调用者。
标签: python-2.7 beautifulsoup python-requests web-crawler