返回

Python爬虫实践代码示例

发布时间:2023-01-30 09:34:45 296
# html# python# 爬虫

对于刚入门爬虫的小伙伴来说,累积经验多练习代码是非常有必要的,下面就是有关爬虫的一些小案例,欢迎大家指正。

import requests
from bs4 import BeautifulSoup
# import pandas

def GetBlogByPage(pageNum):
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
targetUrl="目标网站"
response=requests.get(targetUrl.format(pageNum),headers=headers)
response.encoding='utf-8'
contentText=response.text
soup=BeautifulSoup(contentText,"html.parser")

getTargetInfo=[]
articles=soup.select('.article-item-box')
for article in articles:
info={}
info["title"]=article.a.text.strip()
info["source"]=article.a['href'].strip()
info["sendTime"]=article.div.p.span.text.strip()
info["ReadNum"]=article.div.select('span')[1].text
info["writeNum"]=article.div.select('span')[3].text
getTargetInfo.append(info)
with open("blog.txt",'w') as f:
for info in getTargetInfo:
print(info)
f.write(str(info))
# df=pandas.DataFrame(getTargetInfo)
# df.head()
# df.to_excel('blog.xlsx')


for i in range(1,9): #按页爬取
GetBlogByPage(i)
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线