微专业3个月成为网络爬虫工程师

百度网盘

实例1

功能描述:从CSDN搜索主页,输入keywords,进入kerwords相关的搜索列表页面,手动获取各个url链接,发送新的url请求,获取该网页页面源码,提取标题并保存为html文件。

环境:python3.7+pycharm

库:requests,re, beautifulsoup

分析网页源码:在搜索页面中,查找出列表项的url,每个列表被包含在<dl>标签内,ulr位于其data-track-view属性中。根据列表项的url,进入详情页面,同时找到标题、发布时间分类并提取。其中需要注意的是解码问题,网页源代码编写有不同的编码格式,注意解码。然后搜索列表不止一页,为了继续获取,比较前后两页的url,发现两者的一处不同,p关键字,这里就要用到requests库get方法的params参数,改变url继续进行爬取。

代码:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import requests

import re

from bs4 import BeautifulSoup

url ="https://so.csdn.net/so/search/s.do"

p =0

# s = input()

s ='python'

for pin range(10):

p = p+1

    kv = {'p':'%d' % p, 'q':'%s' % s}

r = requests.get(url, params=kv)

r.encoding ='utf-8'

    so_url = r.request.url

html = r.text

# print(requests.get(so_url).text)

    soup = BeautifulSoup(html, "html.parser")

for dlin soup.find_all('dl'):

text = dl.prettify()

        search_url = dl.get('data-track-view')

search = re.findall(r'"con":"(.*?)"', search_url)[0]

content = requests.get(search).text

# print(content)

        tittle = re.findall(r'<div class="limit_width">\n.*?<a.*?>(.*?)</a>\n.*?<a', text, re.S)[0]

tittle = tittle.replace('<em>', '')

tittle = tittle.replace('</em>', '')

tittle = tittle.replace(' ', '')

tittle = tittle.replace('\n', '')

fb =open('%s.html' % tittle, 'w', encoding='utf-8')

fb.write(content)

print(search, tittle)

#exit()

# print(search)

其中主要

结果如下:

实例2

描述:爬取网络小说章节

代码:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import requests

import re

#下载一个 网页

url ='http://www.17k.com/list/2932117.html'

#模拟浏览器发送http请求

response = requests.get(url)

response.encoding ='utf-8'

html = response.text

#小说标题

tittle = re.findall(r'<h1 class="Title">(.*?)</h1', html)[0]

#print(tittle)

#新建文件保存小说内容

fb =open('%s.txt' % tittle, 'w', encoding ='utf-8')

dl = re.findall(r'<dl class="Volume">.*?</dl>', html, re.S)[0]

dd = re.findall(r'<dd>.*?</dd>', dl, re.S)[0]

#注意正则表达式易错,.不能代替换行符

chapter_info_list = re.findall(r'href="(.*?)".*?>\n.*?<span class="ellipsis.*?">\n\s{60}(.*?)\s{52}<', dd)

#新建文件保存小说内容

#with open('%s.txt' % tittle) as f:

#循环每个章节,分别下载

for chapter_infoin chapter_info_list:

#chapter_tittle = chapter_info[1]

#chapter_url = chapter_info[0]

    chapter_url, chapter_tittle = chapter_info

chapter_url ="http://www.17k.com%s" % chapter_url

#print(chapter_url, chapter_tittle)

#下载章节内容

    chapter_response = requests.get(chapter_url)

chapter_response.encoding ='utf-8'

    chapter_html = chapter_response.text

#读取章节内容

    chapter_content = re.findall(r'<div class="p">(.*?)<div class="author-say"></div>', chapter_html, re.S)[0]

#清洗数据

    chapter_content = chapter_content.replace(' ', '')

chapter_content = chapter_content.replace('&#12288;', '')

chapter_content = chapter_content.replace('<br/>', '')

#持久化

    fb.write(chapter_tittle)

fb.write(chapter_content)

fb.write('\n')

print(chapter_url, chapter_tittle)

#exit()

结果:

中间遇到的问题:关键是正则表达式匹配那部分,.可以匹配的是除“/n"换行符以外的任意字符,html源码标签中存在有换行符,但是我们无法看到,我一开始没有注意到这一问题,导致返回列表为空。

 
发布了21 篇原创文章 · 获赞 43 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/JggAkk/article/details/103866453