python爬虫_requests

爬虫基础

import requests
#引用requests库
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/sanguo.md')
#下载《三国演义》第一回,我们得到一个对象,它被命名为res
res.encoding='utf-8'
#定义Reponse对象的编码为utf-8。
print(res.status_code)
#打印变量res的响应状态码,以检查请求是否成功 200为请求成功
novel=res.text
#把Response对象的内容以字符串的形式返回
print(novel[:800])
#打印小说的前800个字。
'''
response.status_code    检查请求是否成功
response.content         把response对象转换为二进制数据
response.text            把response对象转换为字符串数据
response.encoding        定义response对象的编码

'''
#爬音乐,二进制文件 重点wb
#open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
'''
'r'       open for reading (default)
'w'       open for writing, truncating the file first
'x'       create a new file and open it for writing
'a'       open for writing, appending to the end of the file if it exists
'b'       binary mode
't'       text mode (default)
'+'       open a disk file for updating (reading and writing)
'U'       universal newline mode (deprecated)
'''
import requests
url='https://static.pandateacher.com/Over%20The%20Rainbow.mp3'
res=requests.get(url)
music_marker=res.status_code
print(music_marker)
music=res.content
with open('xx2.mp3','wb') as tt:
    tt.write(music)

顺便引入一些字符知识

 print('臂章'.encode('utf-8'))   #'ASCII'  'gbk'
 print(b'\xe8\x87\x82\xe7\xab\xa0'.decode('utf-8'))
 str.split(str-)   #分割str 目标字符  str- 分割字符
 str+.joint(list)    #合并 list目标列表   str+ 合并添加字符

顺便引入一些文件读写的知识

file = open('path','r',encoding='utf-8') 
file_lines = file.readlines() #注意与read()区别
file.close()

winner = open('path','w',encoding='utf-8') 
winner.writelines(final_scores)  #注意与write(),append()的区别
winner.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。你可以使用 Python 的第三方库 `requests` 和 `beautifulsoup4` 来爬取微博某个话题的内容数据。 首先,你需要了解微博的 URL 格式。以微博话题“Python爬虫”为例,其 URL 为:https://s.weibo.com/weibo?q=%23Python%E7%88%AC%E8%99%AB%23&Refer=top。其中,“%23”是“#”的 URL 编码,“q=”后面跟的是话题的关键词。 接下来,你可以使用 `requests` 库向该 URL 发送 GET 请求,并将响应结果的 HTML 代码解析为 `beautifulsoup4` 对象。例如: ```python import requests from bs4 import BeautifulSoup url = 'https://s.weibo.com/weibo?q=%23Python%E7%88%AC%E8%99%AB%23&Refer=top' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') ``` 然后,你可以使用 `beautifulsoup4` 的查找方法来提取微博内容数据。例如,你可以使用 `find_all` 方法找到所有的微博 div 元素,再从中提取微博的文本内容和发布时间。具体代码如下: ```python weibo_list = soup.find_all('div', class_='content') # 找到所有微博 div 元素 for weibo in weibo_list: text = weibo.find('p', class_='txt').get_text() # 提取微博文本内容 time = weibo.find('p', class_='from').find('a').get_text() # 提取微博发布时间 print(text, time) ``` 以上就是爬取微博话题“Python爬虫”内容数据的基本步骤。当然,实际应用中还需要注意反爬虫策略、数据清洗和存储等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值