贴吧爬虫入门

  

#saved the baidu tieba webpage

import string, urllib2

def baidu_tieba(url,begin_page,end_page):
    for i in range(begin_page,end_page+1,50):
        sName=string.zfill(i,5)+'.html'
        print 'You are downloading the No: ' +str(i)+' web page,and will be saved as '+sName+'.....'
        f=open(sName,'w+')# New open the file with 'w'
        m=urllib2.urlopen(url+str(i)).read()#open the web page and read it
        f.write(m)#write the web page to new file
        f.close()#cloase file


bdurl=str(raw_input(u'please input tieba site, delete pn=number:\n'))
begin_page=int(raw_input('enter start page number:\n'))
end_page=int(raw_input('enter end page number:\n'))

baidu_tieba(bdurl,begin_page,end_page)

  

转载于:https://www.cnblogs.com/sinbad-li/p/5484803.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python来编写一个简单的百度贴吧爬虫。首先,你需要安装requests和BeautifulSoup库。然后,可以按照以下步骤进行操作: 1. 导入所需的库: ```python import requests from bs4 import BeautifulSoup ``` 2. 定义一个函数来获取贴吧的页面内容: ```python def get_page(url): response = requests.get(url) if response.status_code == 200: return response.content else: return None ``` 3. 定义一个函数来解析页面内容,提取所需的信息: ```python def parse_page(content): soup = BeautifulSoup(content, 'html.parser') posts = soup.find_all('div', class_='threadlist_title') for post in posts: title = post.a.text.strip() link = post.a['href'] print('标题:', title) print('链接:', link) ``` 4. 定义一个函数来获取多个页面的内容: ```python def get_multiple_pages(base_url, num_pages): for i in range(1, num_pages+1): url = base_url + '?pn=' + str(i) content = get_page(url) parse_page(content) ``` 5. 设置爬虫的入口点: ```python if __name__ == '__main__': base_url = 'https://tieba.baidu.com/f?kw=python&ie=utf-8' num_pages = 3 # 设置要爬取的页面数量 get_multiple_pages(base_url, num_pages) ``` 在上面的代码中,我们首先定义了一个获取页面内容的函数`get_page()`,然后定义了一个解析页面内容的函数`parse_page()`,接着定义了一个获取多个页面内容的函数`get_multiple_pages()`。最后,在主函数中,我们设置了爬取的入口点。 注意:在进行任何爬取操作时,请确保你遵守网站的使用条款和法律法规,并尊重网站的隐私政策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值