好几个月没写博文了有空来玩玩爬虫之前接触了一个爬虫的项目感触挺深的当时有个爬取巨潮网的操作网上的代码天花乱坠最后还是要靠自己今天这篇算是入门级别欢迎收藏评论。文章目录按默认顺序效果代码解析按时间顺序爬取一次性批量爬取多页内容修改代码效果图爬取多家公司多页数据修改代码效果图(部分)按默认顺序效果代码importrequestsimportre headers{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36}urlhttp://www.baidu.com/s?tnnewsrtt1bsst1cl2wd阿里巴巴# 把链接中rtt参数换成4即是按时间排序默认为1按焦点排序3.4.1小节也有讲到resrequests.get(url,headersheaders).text# 加上headers用来告诉网站这是通过一个浏览器进行的访问# print(res)p_hrefh3 classnews-title_1YtI1 a href(.*?)#提取新闻网址hrefre.findall(p_href,res,re.S)p_titleh3 classnews-title_1YtI1 .*?(.*?)/atitlere.findall(p_title,res,re.S)p_datespan classc-color-gray2 c-font-normal c-gap-right-xsmall .*?(.*?)/spandatere.findall(p_date,res)p_sourcespan classc-color-gray .*?(.*?)/spansourcere.findall(p_source,res)# print(title)# print(href)# print(date)# print(source)#foriinrange(len(title)):# range(len(title)),这里因为知道len(title) 10所以也可以写成for i in range(10)title[i]title[i].strip()# strip()函数用来取消字符串两端的换行或者空格不过目前2020-10并没有换行或空格所以其实不写这一行也没事title[i]re.sub(.*?,,title[i])# 核心用re.sub()函数来替换不重要的内容print(str(i1).title[i](source[i] date[i]))print(href[i])解析重点就是了解正则表达式规则理解非贪婪匹配和贪婪匹配还有就是换行问题的考虑。数据清洗理解sub()函数和strip()函数即可。按时间顺序爬取爬虫的重点就是——网址一定要对上一定是先要有对应的网址然后再去进行后续的操作如果网址都不对那么后面就一定不对做爬虫最重要的就是观察网址的变化以爬取腾讯新闻为例默认的网址http://www.baidu.com/s?tnnewsrtt1bsst1cl2wd‘腾讯’默认是按照焦点排序这里选择时间排序https://www.baidu.com/s?tnnewsrtt4bsst1cl2wd‘腾讯’可以看到它们唯一的区别就在于“rtt”后的数字推断rtt4按照时间rtt1按照焦点排序故将上述代码的url中的rtt修改为4即可。可以看到已经按照时间排序一次性批量爬取多页内容上面讲到了观察那么这里要学的就是“变通”会玩url地址的人或者对url敏感的人学爬虫一般都非常厉害。❄️❄️❄️❄️❄️默认时候的网址https://www.baidu.com/s?tnnewsrtt4bsst1cl2wd%E8%85%BE%E8%AE%AFx_bfe_rqs032000000000000000000000000000000000000000000008x_bfe_tjscore0.080000tngroupnameorganic_newsnewVideo12goods_entry_switch1rsv_dlnews_b_pnpn0️️️删除掉一些内容并做一些处理https://www.baidu.com/s?tnnewsrtt4bsst1cl2wd‘腾讯’pn0爬取第2页https://www.baidu.com/s?tnnewsrtt4bsst1cl2wd‘腾讯’pn10爬取第3页https://www.baidu.com/s?tnnewsrtt4bsst1cl2wd‘腾讯’pn20规律pn按照10、20、30…这样的规律递增num (page - 1) * 10修改代码importrequestsimportreimporttime headers{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36}# 爬取一个公司的多页defbaidu(page):num(page-1)*10# 参数规律是页数-1*10urlhttp://www.baidu.com/s?tnnewsrtt4bsst1cl2wd阿里巴巴pnstr(num)resrequests.get(url,headersheaders).text# 其他相关爬虫代码p_hrefh3 classnews-title_1YtI1 a href(.*?)hrefre.findall(p_href,res,re.S)p_titleh3 classnews-title_1YtI1 .*?(.*?)/atitlere.findall(p_title,res,re.S)p_datespan classc-color-gray2 c-font-normal c-gap-right-xsmall .*?(.*?)/spandatere.findall(p_date,res)p_sourcespan classc-color-gray .*?(.*?)/spansourcere.findall(p_source,res)print(**********title len is {} .format(len(title)))print(**********date len is {}.format(len(date)))print(**********source len is {}.format(len(source)))foriinrange(len(date)):title[i]title[i].strip()# strip()函数用来取消字符串两端的换行或者空格不过这里好像不太需要了title[i]re.sub(.*?,,title[i])# 核心用re.sub()函数来替换不重要的内容print(str(i1).title[i](date[i]-source[i]))print(href[i])foriinrange(3):# 这里一共爬取了3页baidu(i1)# i是从0开始的序号所以要写成i1表示第几页print(第str(i1)页爬取成功)# i是从0开始的序号所以写i1time.sleep(3)# 不要爬太快爬太快会被百度反爬效果图爬取多家公司多页数据修改代码importrequestsimportreimporttime headers{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36}# 爬取多个公司的多页, 可以给函数传入两个参数供参考defbaidu(company,page):num(page-1)*10# 参数规律是页数-1*10urlhttp://www.baidu.com/s?tnnewsrtt4bsst1cl2wdcompanypnstr(num)resrequests.get(url,headersheaders).text# 正则表达式提取内容p_hrefh3 classnews-title_1YtI1 a href(.*?)hrefre.findall(p_href,res,re.S)p_titleh3 classnews-title_1YtI1 .*?(.*?)/atitlere.findall(p_title,res,re.S)p_datespan classc-color-gray2 c-font-normal c-gap-right-xsmall .*?(.*?)/spandatere.findall(p_date,res)p_sourcespan classc-color-gray .*?(.*?)/spansourcere.findall(p_source,res)foriinrange(len(date)):# range(len(title)),这里因为知道len(title) 10所以也可以写成for i in range(10)title[i]title[i].strip()# strip()函数用来取消字符串两端的换行或者空格不过这里好像不太需要了title[i]re.sub(.*?,,title[i])# 核心用re.sub()函数来替换不重要的内容print(str(i1).title[i](date[i]-source[i]))print(href[i])companys[阿里巴巴,万科集团,百度集团,腾讯,京东]forcompanyincompanys:foriinrange(3):# 这里一共爬取了3页baidu(company,i1)# i是从0开始的序号所以要写成i1表示第几页print(company第str(i1)页爬取成功)# i是从0开始的序号所以写i1time.sleep(3)# 不要爬太快爬太快会被百度反爬效果图(部分)后面不再赘述️️️