Python 爬虫(抓取网页内容简单实现)

305次阅读
没有评论

1. 首先第一步我们先找到自己抓取的网站网址以及内容

在这里我使用的是 https://m.douban.com/group/729027/

抓取的内容是这个网页下的:

Python

所有的讨论

2. 对这个网页的html进行解析,找到讨论这一栏的html源码

使用F12对当前页面进行解析:

Python

点击圈起来的部分后,点击讨论中的 “婉卿……”  右边就能自动跳转到这一句的源码了

右键单击源码后点击复制中的 复制selector

复制出来的是: #group-topics > div:nth-child(2) > table > tbody > tr:nth-child(2) > td.title > a

这个可以理解为这句评论在html中的地址

多复制几个其他的讨论找到规律:

#group-topics > div:nth-child(2) > table > tbody > tr:nth-child(5) > td.title > a

发现后三位主要就是tr:nth-child不一样,那么我们就取 tr td.title a 作为我们想要的选择条件

3. 使用python开始编写代码

from urllib.request import urlopen, Request from bs4 import BeautifulSoup import xlwt

url = input('Please enter the URL here:') headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'} ret = Request(url, headers=headers) res = urlopen(ret) aa = res.read().decode('utf-8')

soup = BeautifulSoup(aa,'html.parser') comment = soup.select('tr td.title a') for i in range(0,len(comment)): comment[i] = comment[i].get('title')

代码的简单原理就是用你的电脑模拟访问网页并且获得服务器返回的html源码

BeautifulSoup是python使用爬虫时的一个包。使用我们刚刚拿到的selector,将整个页面的html代码过滤,得到想要的部分.

我们在刚刚在网页看到的html源码里面可以看到:

Python

这个评论是title底下, 所以使用 get('title') 获得title里面的值。最后comment就是我们想要的评论啦

4. 补充

如果遇到这样的html,怎么获取“小悠哉”这个名字呢?

<a href="https://www.douban.com/people/175925841/?_dtcc=1" class="">小悠哉</a>

同样使用selector拿到这一整片的代码,然后使用 .string 就可以啦

soup = BeautifulSoup(aa,'html.parser') comment = soup.select('tr td.title a') for i in range(0,len(comment)): comment[i] = comment[i].get('title')

author = soup.select('td:nth-child(2) a') for i in range(0,len(author)): author[i] = author[i].string

count = soup.select('tr td.r-count') for i in range(0,len(count)): count[i] = count[i].string

 

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-24发表,共计1665字。
新手QQ群:570568346,欢迎进群讨论 Python51学习