首页 > 编程语言 >Python爬虫实践代码示例

Python爬虫实践代码示例

时间:2023-01-28 10:31:30浏览次数:38  
标签:info 示例 Python text 爬虫 df article getTargetInfo div

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

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)

标签:info,示例,Python,text,爬虫,df,article,getTargetInfo,div
From: https://blog.51cto.com/u_13488918/6024759

相关文章

  • ifc4x3 附录E示例-Georeference_Tin_2
    ifc4x3 附录E示例-Georeference_Tin_2示例概述意图IFC4x3RC1此场景中,所有三角形都只有一种颜色的锡。没有空隙。锡的坐标为地图比例,但其本地原点为(256400.0、701160......
  • Python入门之转义符
    """转义符:改变原始字符含义的特殊符号"""#不支持所见即所得name="黎二狗"name='黎二狗'#可见即所得name='''黎二狗'''name="""黎......
  • python入门之str/ord/chr
    """字符串(str):由一系列字符组成的不可变系列容器,存储的是字符的编码值。编码:1.字节byte:计算机最小存储单位,等于8位bit。(bitBKBMBGT)2.字符:单个的数......
  • Python中得可变哈希不可变哈希
    类型与哈希哈希(散列计算),可以将任意长度的输出,通过散列算法变为固定长度输出,简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。​​1.可哈希......
  • Python字符串
    1.replace替换#replace实现字符串替换a='sjbfnjajjkdgbnv'a=a.replace('f','里')print(a)out:sjb里njajjkdgbnv整个过程,实际是创建了新的字符串对象,并指向了变量a,并......
  • [转]python发邮件smtplib和email模块详解
    本文转自:https://www.cnblogs.com/auguse/articles/14578247.htmlSMTP(SimpleMailTransferProtocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,......
  • [转]用Python自动化操作PPT
    本文转自:https://jishuin.proginn.com/p/763bfbd771b6作者:超级大洋葱806https://tangxing.blog.csdn.net/article/details/1095688301.PPT自动化能干什么?有什么优势?它......
  • [转]利用python的PyPDF2和PyMuPDF库玩转PDF的提取、合并、旋转、缩放、加密
    本文转自:https://www.cnblogs.com/steven0325/p/16888425.html一、安装PyPDF2和PyMuPDF库pipinstallPyPDF2pipinstallpymupdf#fitz是pymupdf的子模块二、工具......
  • [转]python库 Pywin32使用
    本文转自:https://www.cnblogs.com/chenjy1225/p/12174889.htmlpython库Pywin32使用 https://github.com/wuxc/pywin32docPywin32提供了很多访问windows的API。较重......
  • ifc4x3 附录E示例-Georeference_Tin_1
    ifc4x3 附录E示例-Georeference_Tin_1示例概述意图IFC4x3RC1这个场景中有一个锡(只有1.5K个三角形),所有三角形只有一种颜色。地图投影MGA9456区的坐标为东距、北距和......