python新手爬取论坛贴吧特定人的帖子——虎扑《健美大神之路》
2017-08-21 23:01
218 查看
在虎扑上,有博主翻译《健美大神之路》,感觉很好,但是想要找电子书却没有,所以就打算自己爬下来存在文本文档中。
我应用的是urllib2,beautifulsoup这两个工具。
在这个编程中,我遇到的最大的麻烦就是,编码标准错误和我爬取的帖子文本中有他人的帖子。
第一个问题我现在还是不太懂,最后胡乱试解决了。 for string in tags.next_sibling.next_sibling.find('div',class_='quote-content').strings:
string_gbk=string.encode('utf-8')
file.write(string_gbk)
第二行如果不用方法。encode('utf-8'),就会报出gbk读码错误。
第二个问题主要要解决的便是找出特定人发的帖子和别人发的帖子,在html源中有什么不同,然后限定条件。
url='https://bbs.hupu.com/19201877.html'
一开始我找的是
这样的标签,然后筛选出其中的strings
因为所有人发的帖子都在这样的标签中所以自然的就都爬了。
得到这样的原因后,我就仔细的查看和比较如何才能找出我想要的
然后我发现了一个显而易见的逻辑,那便是帖子的头部都会有发帖人的信息,所以这就是突破口。
虽然有了正确的方向,也有了正确的工具beautifulsoup的兄弟节点,但是我在兄弟节点的处理也就是整个html的逻辑树的结构上的认识错误导致我一直出现错误,其中有两点我觉得十分重要:
1.
In real documents, the
You might think that the
9d35
ing: the comma andnewline that separate the first <a> tag from the second:
The second <a> tag is actually the
bs官方文档对兄弟节点一个常见错误的解释。
2.一定要从大到小的顺序观察html的标签结构(节点结构),浏览器的检查元素功能要比直接查看源好。
最后放代码了,只是小的程序,所以没有按照工程结构写,推荐自己建工程定义模块定义类的方式,可以自己的逻辑和面向对象的思维有好处。# -*- coding:utf-8 -*-
import urllib2
from bs4 import BeautifulSoup
file=open('book.txt','w')
start_url='https://bbs.hupu.com/19201877.html'
all_urls=[]
all_urls.append(start_url)
for x in range(2,6):
all_urls.append('https://bbs.hupu.com/19201877-'+str(x)+'.html')
for url in all_urls:
request=urllib2.Request(url)
response=urllib2.urlopen(request)
cont=response.read()
soup=BeautifulSoup(cont,"lxml",from_encoding='utf-8')
for tags in soup.find_all('div',class_="author"):
# print tags.next_sibling.next_sibling.find('div',class_='quote-content')
if tags.div.a['href']=='https://my.hupu.com/232157742256797':
for string in tags.next_sibling.next_sibling.find('div',class_='quote-content').strings:
string_gbk=string.encode('utf-8')
file.write(string_gbk)
我应用的是urllib2,beautifulsoup这两个工具。
在这个编程中,我遇到的最大的麻烦就是,编码标准错误和我爬取的帖子文本中有他人的帖子。
第一个问题我现在还是不太懂,最后胡乱试解决了。 for string in tags.next_sibling.next_sibling.find('div',class_='quote-content').strings:
string_gbk=string.encode('utf-8')
file.write(string_gbk)
第二行如果不用方法。encode('utf-8'),就会报出gbk读码错误。
第二个问题主要要解决的便是找出特定人发的帖子和别人发的帖子,在html源中有什么不同,然后限定条件。
url='https://bbs.hupu.com/19201877.html'
一开始我找的是
<div class="quote-content">
这样的标签,然后筛选出其中的strings
因为所有人发的帖子都在这样的标签中所以自然的就都爬了。
得到这样的原因后,我就仔细的查看和比较如何才能找出我想要的
<div class="quote-content">
然后我发现了一个显而易见的逻辑,那便是帖子的头部都会有发帖人的信息,所以这就是突破口。
虽然有了正确的方向,也有了正确的工具beautifulsoup的兄弟节点,但是我在兄弟节点的处理也就是整个html的逻辑树的结构上的认识错误导致我一直出现错误,其中有两点我觉得十分重要:
1.
In real documents, the
.next_siblingor
.previous_siblingof atag will usually be a string containing whitespace. Going back to the“three sisters” document:
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a> <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
You might think that the
.next_siblingof the first <a> tag wouldbe the second <a> tag. But actually, it’s a str
9d35
ing: the comma andnewline that separate the first <a> tag from the second:
link = soup.a lin # <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> link.next_sibling # u',\n'
The second <a> tag is actually the
.next_siblingof the comma:
link.next_sibling.next_sibling # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
bs官方文档对兄弟节点一个常见错误的解释。
2.一定要从大到小的顺序观察html的标签结构(节点结构),浏览器的检查元素功能要比直接查看源好。
最后放代码了,只是小的程序,所以没有按照工程结构写,推荐自己建工程定义模块定义类的方式,可以自己的逻辑和面向对象的思维有好处。# -*- coding:utf-8 -*-
import urllib2
from bs4 import BeautifulSoup
file=open('book.txt','w')
start_url='https://bbs.hupu.com/19201877.html'
all_urls=[]
all_urls.append(start_url)
for x in range(2,6):
all_urls.append('https://bbs.hupu.com/19201877-'+str(x)+'.html')
for url in all_urls:
request=urllib2.Request(url)
response=urllib2.urlopen(request)
cont=response.read()
soup=BeautifulSoup(cont,"lxml",from_encoding='utf-8')
for tags in soup.find_all('div',class_="author"):
# print tags.next_sibling.next_sibling.find('div',class_='quote-content')
if tags.div.a['href']=='https://my.hupu.com/232157742256797':
for string in tags.next_sibling.next_sibling.find('div',class_='quote-content').strings:
string_gbk=string.encode('utf-8')
file.write(string_gbk)
相关文章推荐
- Python爬虫抓取虎扑论坛帖子图片
- python虎扑社区论坛数据爬虫分析报告
- Python2.7 自动抓取某贴吧所有帖子的图片
- Python爬虫——抓取贴吧帖子
- 【python项目实战】BBS论坛(5)帖子列表展示
- python爬虫-爬取股票贴吧帖子
- python爬取某个贴吧帖子留下的邮箱,并自动发送相应链接
- Python爬取某贴吧第一页的所有帖子的标题、连接、作者,将数据储存到txt文件中
- 爬虫 监控 自动 匹配 抓取 卡饭 论坛 帖子 发邮件 python
- Python爬虫实战(1):爬取Drupal论坛帖子列表
- Python 抓取贴吧里某楼主所有的帖子
- Python爬虫学习:我的第一个爬虫—爬取贴吧感兴趣帖子的图片(大家都懂,哈哈)
- Python爬虫实战(1):爬取Drupal论坛帖子列表
- Python爬虫爬取贴吧的帖子内容
- 【新手】python爬虫遍历贴吧用户
- Python爬虫实战(1):爬取Drupal论坛帖子列表
- Python新手学习基础之初识python——与众不同2
- 2005-1-20+ 开源论坛stella公开+新手指南
- 简单配置nginx+uWSGI+python3+bottle环境新手教程
- 17个新手常见Python运行时错误