领导知道我会点py。让我去几个网站爬文章下来。
然后我开始百度py怎么爬爬虫。
我的思路如下(初学者抛砖引玉)
1、首先爬取网站所有连接;
2、然后依次访问连接爬取数据;
3、随后把数据清洗后保存到txt里面;
4、打包发给运营。
# coding=utf-8
from bs4 import BeautifulSoup
import requests
def getHtml(url, label, attr):
response = requests.get(url)
response.encoding = 'utf-8'
html = response.text
soup = BeautifulSoup(html, 'html.parser')
for target in soup.find_all(label):
try:
value = target.get(attr)
except:
value = ''
if value:
print(value)
url = 'http://www.miit.gov.cn/n1146290/n1146392/index.html'
label = 'a'
attr = 'href'
getHtml(url, label, attr)
筛查爬取到的连接,发现这些分页打开能看到具体的文章连接↓