Python抓取知乎作者主页文章

import requests # 使用headers是一种习惯 headers={‘user-agent’:’Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36′} url=’https://www.zhihu.com/api/v4/members/zhang-jia-wei/articles?’ # 建立一个空列表,以待写入数据 articlelist=[] # 设置offset的起始值为第一页的值:10 offset=10 while True: # 封装参数 params={ ‘include’:’data[*].comment_count,suggest_edit,is_normal,thum…

Read More

Python 配合openpyxl模块 把抓取到的数据写入xlsx EXCEL表格

import requests,openpyxl # 创建工作薄 wb=openpyxl.Workbook() # 获取工作薄的活动表 sheet=wb.active # 工作表重命名 sheet.title=’lyrics’ sheet[‘A1′] =’歌曲名’ # 加表头,给A1单元格赋值 sheet[‘B1′] =’所属专辑’ # 加表头,给B1单元格赋值 sheet[‘C1′] =’播放时长’ # 加表头,给C1单元格赋值 sheet[‘D1′] =’播放链接’ # 加表头,给D1单元格赋值 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ for x in range(5): params = { ‘ct’: ’24’, ‘qqmusic_ver’: ‘1298’, ‘new_json’: ‘1’, ‘remote…

Read More

python 获取QQ音乐周杰伦前5页歌曲的歌词

# 直接运行代码就好 from bs4 import BeautifulSoup import requests # 引用requests模块 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ headers = { ‘origin’:’https://y.qq.com’, # 请求来源,本案例中其实是不需要加这个参数的,只是为了演示 ‘referer’:’https://y.qq.com/n/yqq/song/004Z8Ihr0JIu5s.html’, # 请求来源,携带的信息比“origin”更丰富,本案例中其实是不需要加这个参数的,只是为了演示 ‘user-agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like …

Read More

Python 抓取QQ音乐歌手所有音乐(以周杰伦为例)

# 直接运行代码就好 import requests # 引用requests模块 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ headers = { ‘origin’:’https://y.qq.com’, # 请求来源,本案例中其实是不需要加这个参数的,只是为了演示 ‘referer’:’https://y.qq.com/n/yqq/song/004Z8Ihr0JIu5s.html’, # 请求来源,携带的信息比“origin”更丰富,本案例中其实是不需要加这个参数的,只是为了演示 ‘user-agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Saf…

Read More

Python 最简单的抓取单个网页并把数据写入记事本

import requests #发出请求,并把返回的结果放在变量res中 res = requests.get(‘https://localprod.pandateacher.com/python-manuscript/crawler-html/exercise/HTTP%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81.md’) #打印状态码 200代表成功 print(res.status_code) #把Reponse对象的内容以字符串的形式返回 data=res.text #打开文件 没有则会自动新建abc.txt txt = open(‘abc.txt’,’w’) txt.write(data) txt.close() #关闭文件

Python +gevent 多线程抓取网站数据

from gevent import monkey #从gevent库里导入monkey模块。 monkey.patch_all() #monkey.patch_all()能把程序变成协作式运行,就是可以帮助程序实现异步。 import gevent,time,requests #导入gevent、time、requests。 start = time.time() #记录程序开始时间。 url_list = [‘https://www.baidu.com/’, ‘https://www.sina.com.cn/’, ‘http://www.sohu.com/’, ‘https://www.qq.com/’, ‘https://www.163.com/’, ‘http://www.iqiyi.com/’, ‘https://www.tmall.com/’, ‘http://www.ifen…

Read More

Python 自动抓取天气预报并且每天自动发送天气到指定邮箱

import requests import smtplib import schedule import time from bs4 import BeautifulSoup from email.mime.text import MIMEText from email.header import Header # account = input(‘请输入你的邮箱:’) # password = input(‘请输入你的密码:’) # receiver = input(‘请输入收件人的邮箱:’) account = ‘121942198@qq.com’ password = ” #填写QQ密码,非登录密码,详见QQ邮箱设置里 receiver = ‘76123708@qq.com’ def weather_spider(): headers={‘user-agent’:’Mozilla…

Read More

Python+selenium +BeautifulSoup抓取QQ音乐精彩评论(涉及点击加载更多的处理方式)

# 本地Chrome浏览器设置方法 from selenium import webdriver import time #本地一定得在环境根目录下安装谷歌浏览器的驱动 driver = webdriver.Chrome() from bs4 import BeautifulSoup import time driver.get(‘https://y.qq.com/n/yqq/song/000xdZuV2LcQ19.html’) # 访问页面 time.sleep(2) #点击3次页面的‘点击加载更多’按钮 加上默认的15条数据,最后就有60条热评了 for i in range(3): button = driver.find_element_by_class_name(‘js_get_more_hot’) # 根据类名找到【点击加载更多】 button.click() # 点击 tim…

Read More

Python selenium提取数据的方法

# 以下方法都可以从网页中提取出’你好,蜘蛛侠!’这段文字 find_element_by_tag_name:通过元素的名称选择 # 如<h1>你好,蜘蛛侠!</h1> # 可以使用find_element_by_tag_name(‘h1’) find_element_by_class_name:通过元素的class属性选择 # 如<h1 class=”title”>你好,蜘蛛侠!</h1> # 可以使用find_element_by_class_name(‘title’) find_element_by_id:通过元素的id选择 # 如<h1 id=”title”>你好,蜘蛛侠!</h1> # 可以使用find_element_by_id(‘title’) find_element_by_name:通过元素的name…

Read More