爬虫python能做什么(爬虫python)

导读 你们好,最近小活发现有诸多的小伙伴们对于爬虫python能做什么,爬虫python这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看

你们好,最近小活发现有诸多的小伙伴们对于爬虫python能做什么,爬虫python这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看吧。

1、基础爬虫的固定模式

2、我们这里说的基础爬虫,是指无需处理验证码、代理、异象异步加载等高阶爬虫技术的爬虫形式。通常来说,基础爬虫的两大请求库 urllib 和 requests 中 requests 一般是被绝大部分人所喜欢的,即使Urllib的功能也算齐全。两大解析库 BeautifulSoup 由于本身强大的Html文档解析能力而深受喜爱,另外一种解析库 lxml 在搭配 xpath 表达式的基础上也大大提升了效率。就基础爬虫来讲,两大请求库和两大解析库的组合方式能根据个人喜好去选择。

3、比较常用的爬虫组合工具是:

4、requests + BeautifulSoup

5、requests + lxml

6、同一网页爬虫的四种实现方式

7、假如要抓取每条新闻的标题和链接,并把其组合为一个字典的结构打印出来。第一步查看Html源码明确新闻标题信息组织结构。

8、能够目标信息存在于 em 标签下 a 标签内的文本和 href 属性中。可随时借助 requests 库构造请求,并用 BeautifulSoup 或者 lxml 进行解析。

9、方式一: requests + BeautifulSoup + select css选择器

10、# select method

11、import requests

12、from bs4 import BeautifulSoup

13、headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}

14、url = 'http://news.qq.com/'Soup = BeautifulSoup(requests.get(url=url, headers=headers).text.encode("utf-8"), 'lxml')

15、em = Soup.select('em[class="f14 l24"] a')for i in em:

16、title = i.get_text()

17、link = i['href'] print({'标题': title,

18、'链接': link

19、})select method

20、import requests

21、from bs4 import BeautifulSoup

22、headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}

23、url = 'http://news.qq.com/'Soup = BeautifulSoup(requests.get(url=url, headers=headers).text.encode("utf-8"), 'lxml')

24、em = Soup.select('em[class="f14 l24"] a')for i in em:

25、title = i.get_text()

26、link = i['href'] print({'标题': title,

27、'链接': link})

28、方式二:requests+BeautifulSoup+find_all进行信息提取

29、# find_all method

30、import requests

31、from bs4 import BeautifulSoup

32、headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'}

33、url = 'http://news.qq.com/'

34、Soup = BeautifulSoup(requests.get(url=url, headers=headers).text.encode("utf-8"), 'lxml')

35、em = Soup.find_all('em', attrs={'class': 'f14 l24'})for i in em:

36、title = i.a.get_text()

37、link = i.a['href']

38、print({'标题': title,

39、'链接': link})

40、同样是 requests + BeautifulSoup 的爬虫组合,但在信息提取上采用了 find_all 的方式。九州IP能让你随时切换需要的IP地址。

以上就是爬虫python这篇文章的一些介绍,希望对大家有所帮助。

标签:

免责声明:本文由用户上传,如有侵权请联系删除!

Baidu
map