在 HTML中 <a href='xx'> 表示超链接,所以要是提取页面 url 的话就是提取 ‘xx’
方法一:find_all
import urllib
import requests
from urllib.parse import urlparse
from urllib import request, parse
from bs4 import BeautifulSoup
word = '周杰伦'
# word为关键词,pn是百度用来分页的..
url = 'http://www.baidu.com.cn/s?wd=' + urllib.parse.quote(word) + '&pn=0'
print(url)
# 通过 url 获取域名
res = urlparse(url)
domain = res.netloc
print(domain)
print('- - '*30)
response = request.urlopen(url)
page = response.read()
soup = BeautifulSoup(page, 'lxml')
# tagh3 = soup.find_all('h3') # 返回 list
tagh3 = soup.find_all('a') # 获取所有 a 标签下内容,返回 list
all = open(r'F:\security\web\output\report\test.txt', 'w+')
hrefs = []
for h3 in tagh3:
# href = h3.find('a').get('href')
try:
href = h3.get('href') # 获取 a 标签下 href 的属性值(即:url)
except:
pass
else:
hrefs.append(href)
hrefs = list({}.fromkeys(hrefs).keys()) # 去重
for href in hrefs:
if href == None:
Python3 爬虫提取HTML中所有真实URL方法

本文介绍了如何使用Python3进行网页爬虫,通过BeautifulSoup库来提取请求页面中的所有真实URL。讲解了两种方法,包括find_all函数和CSS选择器select的使用技巧。
最低0.47元/天 解锁文章
1261

被折叠的 条评论
为什么被折叠?



