import requests
from bs4 import BeautifulSoup
import json
def get_data(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
def parse_data(html):
ss = None
try:
soup = BeautifulSoup(html, 'lxml')
for s in soup.findAll(name="div", attrs={"class": "article-item-box csdn-tracking-statistics"}):
for ss in s.findAll(name="h4"):
sss = ss.find(name="a", href=True);
con = {
"文章标题":ss.getText(),
"文章链接":sss['href']
}
save_data(con)
e
python爬取博客文章示例
最新推荐文章于 2024-03-22 07:50:50 发布