python爬取古诗词诗句

1.如何用python 写一个爬虫用来爬P 站特定标签下的高赞作品

Python写这些爬虫,批量获取还是挺简单的……不长逛P站但是写了个可以获取知乎指定收藏夹下的高赞答案的东西……

获取知乎指定收藏夹下的高赞答案

获取P站特定标签下的高赞作品

这两个需求应该是差不多的……

爬取收藏夹的代码大概是这样:

__author__ = '7sDream'

import zhihu

collection = zhihu.Collection('')

for answer in collection.answers:

if answer.upvote >= 1000:

answer.save(path=collection.name)

结果是这样:

理论上来说爬P站应该差不多……

做好cookies管理,HTML解析就差不多了。

如果Python会的差不多了看看这个知乎模块的源码应该能写出来P站的

2.如何用python爬取一本书的评论用户

京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。

以购买日期为例,使用Python + Mysql的搭配进行实现,程序不大,才100行。相关的解释我都在程序里加注了:from selenium import webdriverfrom bs4 import BeautifulSoupimport reimport win32com.clientimport threading,timeimport MySQLdbdef mydebug():driver.quit()exit(0)def catchDate(s):"""页面数据提取"""soup = BeautifulSoup(s)z = []global nowtimesm = soup.findAll("div",class_="date-buy")for obj in m:try:tmp = obj.find('br').contentsexcept Exception, e:continueif(tmp != ""):z.append(tmp)nowtimes += 1return zdef getTimes(n,t):"""获取当前进度"""return "当前进度为:" + str(int(100*n/t)) + "%"#———————————————————————————————————| 程序开始 |—————————————————————————————————#确定图书大类cate = {"3273":"历史","3279":"心理学","3276":"政治军事","3275":"国学古籍","3274":"哲学宗教","3277":"法律","3280":"文化","3281":"社会科学"}#断点续抓num1 = input("bookid:")num2 = input("pagenumber:")#生成图书大类链接,共需17355*20 = 347100次totaltimes = 347100.0nowtimes = 0#开启webdirver的PhantomJS对象#driver = webdriver.PhantomJS()driver = webdriver.Ie('C:\Python27\Scripts\IEDriverServer')#driver = webdriver.Chrome('C:\Python27\Scripts\chromedriver')#读出Mysql中的评论页面,进行抓取# 连接数据库 try:conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='jd')except Exception, e:print esys.exit()# 获取cursor对象cursor = conn.cursor()sql = "SELECT * FROM booknew ORDER BY pagenumber DESC"cursor.execute(sql)alldata = cursor.fetchall()flag = 0flag2 = 0# 如果有数据返回就循环输出,htt/review/10178500-1-154.htmlif alldata:for rec in alldata:#rec[0]--bookid,rec[1]--cateid,rec[2]--pagenumberif(rec[0] != str(num1) and flag == 0):continueelse:flag = 1for p in range(num2,rec[2]):if(flag2 == 0):num2 = 0flag2 = 1p += 1link = "htteview/" + rec[0] + "-1-" + str(p) + ".html"#抓网页driver.get(link)html = driver.page_source#抓评论buydate = catchDate(html)#写入数据库for z in buydate:sql = "INSERT INTO ljj (id, cateid, bookid, date) VALUES (NULL, '" + rec[0] + "','" + rec[1] + "','" + z[0] + "');"try:cursor.execute(sql)except Exception, e:print econn.commit()print getTimes(nowtimes,totaltimes)driver.quit()cursor.close()conn.close() 京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。

以购买日期为例,使用Python + Mysql的搭配进行实现,程序不大,才100行。相关的解释我都在程序里加注了:from selenium import webdriverfrom bs4 import BeautifulSoupimport reimport win32com.clientimport threading,timeimport MySQLdbdef mydebug():driver.quit()exit(0)def catchDate(s):"""页面数据提取"""soup = BeautifulSoup(s)z = []global nowtimesm = soup.findAll("div",class_="date-buy")for obj in m:try:tmp = obj.find('br').contentsexcept Exception, e:continueif(tmp != ""):z.append(tmp)nowtimes += 1return zdef getTimes(n,t):"""获取当前进度"""return "当前进度为:" + str(int(100*n/t)) + "%"#———————————————————————————————————| 程序开始 |—————————————————————————————————#确定图书大类cate = {"3273":"历史","3279":"心理学","3276":"政治军事","3275":"国学古籍","3274":"哲学宗教","3277":"法律","3280":"文化","3281":"社会科学"}#断点续抓num1 = input("bookid:")num2 = input("pagenumber:")#生成图书大类链接,共需17355*20 = 347100次totaltimes = 347100.0nowtimes = 0#开启webdirver的PhantomJS对象#driver = webdriver.PhantomJS()driver = webdriver.Ie('C:\Python27\Scripts\IEDriverServer')#driver = webdriver.Chrome('C:\Python27\Scripts\chromedriver')#读出Mysql中的评论页面,进行抓取# 连接数据库 try:conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='jd')except Exception, e:print esys.exit()# 获取cursor对象cursor = conn.cursor()sql = "SELECT * FROM booknew ORDER BY pagenumber DESC"cursor.execute(sql)alldata = cursor.fetchall()flag = 0flag2 = 0# 如果有数据返回就循环输出,httreview/10178500-1-154.htmlif alldata:for rec in alldata:#rec[0]--bookid,rec[1]--cateid,rec[2]--pagenumberif(rec[0] != str(num1) and flag == 0):continueelse:flag = 1for p in range(num2,rec[2]):if(flag2 == 0):num2 = 0flag2 = 1p += 1link = "ht.com/review/" + rec[0] + "-1-。

3.如何使用Python爬取网易云音乐歌单内的歌曲

1首先在浏览器在打开网易云音乐的网页版,并点进一个歌单。

2.在浏览器的开发者工具审查该页面的元素(一般按f12可以弹出该工具),选择Network,之后选择doc可以简便地找到我们需要的元素。

注:以下两步第3,第4步主要是为了防止网站的反爬,经过这两步后,基本可以避开网站的反爬,所以如果你爬取的网站没有反爬的话,可以跳过这两步。

3.查看网页的请求方式--get请求

4.查看header

5.在Response中找到我们所需要的元素所在的标签位置(如:我们要爬取的是歌曲,既需要寻找歌曲的名字与其id信息,观察可以发现其中一首歌单中的歌名-moon在类名为“f-hide”的ul标签的li标签的a标签中) ~

4.Python如何爬取网页文本内容

可以安装BeautifulSoup,然后查找DOM找到你要的文本标签例如是文本内容使用get_text()我用的python3from bs4 import BeautifulSoupfrom request。

urllib import urlopenhtml = urlopen("网址")bsObj =BeautifulSoup(html,"html。parser")getP = bsObj。

find("div",{"id":"content"})。 find("p")contentText = getP。

get_text()#contentText就是你要的文本内容print(ccontentText)可能会有错误,大概就是这么个意思可能你用别的库,或者python2会有点区别你还不会抓取文本内容,应该刚学,建议你先把基础内容学好,看书《python网络数据采集》,看教程"崔庆才的个人网站"这些东西网上很好查的,多查,查不到了再问。 后面难的东西还很多,要想自学就要多自己动手祝你学有所成。

5.如何用Python爬取学校

下面介绍login(self,k)方法def login1(self,k):user= str(123456789+k)#用户名为学号,为保护隐私,采用该输入,K位偏移量,这是由于学号采用递增方式排列passwd= str(123456789+k)#初始密码等于用户名#设置提交给服务器的数据postdata= urllib.urlencode({'user':user,'pass':passwd,'typeid':'2'})#typeid为用户种类request=urllib2.Request(url=self.loginurl,data = postdata,headers=self.headers)result= self.opener.open(request)#登陆并获取返回内容return user#返回学号登陆成功之后,由于cookie已存在并记录在签名定义的cookie中,因此可以直接访问信息页面,调用方法getInfPage(self):def getInfPage(self):request=urllib2.Request(url=self.infurl,headers=self.headers)result= self.opener.open(request)return result.read().decode('gbk')1234567读取的数据为html的文本,因此需要使用正则表达式对需要的内容进行抓取,调用方法readInf(self):def readInf(self):page= self.getInfPage()#page为html内容inf=re.findall('([^].*?).*?(.*?)',page,re.S)return inf12345按照规则匹配后数据将会存在list中并返回,此时调用writeinf(self,Items,user)方法讲信息写入excel 评论0 0 0 最新回答 (1条回答) 梨落。

1级 2018-11-19 回答 import requestsfrom lxml import html# 创建 session 对象。这个对象会保存所有的登录会话请求。

session_requests = requests.session()# 提取在登录时所使用的 csrf 标记login_url = ".fromstring(result.text)authenticity_token = list(set(tree.xpath("//input[@name='csrfmiddlewaretoken']/@value")))[0]payload = { "username": "", "password": "", "csrfmiddlewaretoken": authenticity_token # 在源代码中,有一个名为 “csrfmiddlewaretoken” 的隐藏输入标签。}# 执行登录result = session_requests.post( login_url, data = payload, headers = dict(referer=login_url))# 已经登录成功了,然后从 bitbucket dashboard 页面上爬取内容。

url = '.fromstring(result.content)bucket_elems = tree.findall(".//span[@class='repo-name']/")bucket_names = [bucket.text_content.replace("n", "").strip() for bucket in bucket_elems]print(bucket_names) 评论0 0 0 你想知道的这里都有 已解决问题:262,007,030 新手帮助 如何提问 如何回答 权威合作 企业合作 在线咨询 投诉建议 举报不良信息 意见反馈及投诉 搜狗问问小程序 企业推广 – 输入法 – 浏览器 – 隐私政策 – 免责声明 – 用户协议 – 帮助 © 2020 SOGOU.COM 京ICP证050897号 import requestsfrom lxml import html# 创建 session 对象。这个对象会保存所有的登录会话请求。

session_requests = requests.session()# 提取在登录时所使用的 csrf 标记login_url = ".fromstring(result.text)authenticity_token = list(set(tree.xpath("//input[@name='csrfmiddlewaretoken']/@value")))[0]payload = { "username": "", "password": "", "csrfmiddlewaretoken": authenticity_token # 在源代码中,有一个名为 “csrfmiddlewaretoken” 的隐藏输入标签。}# 执行登录result = session_requests.post( login_url, data = payload, headers = dict(referer=login_url))# 已经登录成功了,然后从 bitbucket dashboard 页面上爬取内容。

url = '.fromstring(result.content)bucket_elems = tree.findall(".//span[@class='repo-name']/")bucket_names = [bucket.text_content.replace("n", "").strip() for bucket in bucket_elems]print(bucket_names)。

6.如何用python爬取一本书的评论用户

京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。

以购买日期为例,使用Python + Mysql的搭配进行实现,程序不大,才100行。相关的解释我都在程序里加注了: from selenium import webdriverfrom bs4 import BeautifulSoupimport reimport win32com.clientimport threading,timeimport MySQLdbdef mydebug():driver.quit()exit(0)def catchDate(s):"""页面数据提取"""soup = BeautifulSoup(s)z = []global nowtimesm = soup.findAll("div",class_="date-buy")for obj in m:try:tmp = obj.find('br').contentsexcept Exception, e:continueif(tmp != ""):z.append(tmp)nowtimes += 1return zdef getTimes(n,t):"""获取当前进度"""return "当前进度为:" + str(int(100*n/t)) + "%"#———————————————————————————————————| 程序开始 |—————————————————————————————————#确定图书大类cate = {"3273":"历史","3279":"心理学","3276":"政治军事","3275":"国学古籍","3274":"哲学宗教","3277":"法律","3280":"文化","3281":"社会科学"}#断点续抓num1 = input("bookid:")num2 = input("pagenumber:")#生成图书大类链接,共需17355*20 = 347100次totaltimes = 347100.0nowtimes = 0#开启webdirver的PhantomJS对象#driver = webdriver.PhantomJS()driver = webdriver.Ie('C:\Python27\Scripts\IEDriverServer')#driver = webdriver.Chrome('C:\Python27\Scripts\chromedriver')#读出Mysql中的评论页面,进行抓取# 连接数据库 try:conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='jd')except Exception, e:print esys.exit()# 获取cursor对象cursor = conn.cursor()sql = "SELECT * FROM booknew ORDER BY pagenumber DESC"cursor.execute(sql)alldata = cursor.fetchall()flag = 0flag2 = 0# 如果有数据返回就循环输出,htt/review/10178500-1-154.htmlif alldata:for rec in alldata:#rec[0]--bookid,rec[1]--cateid,rec[2]--pagenumberif(rec[0] != str(num1) and flag == 0):continueelse:flag = 1for p in range(num2,rec[2]):if(flag2 == 0):num2 = 0flag2 = 1p += 1link = "htteview/" + rec[0] + "-1-" + str(p) + ".html"#抓网页driver.get(link)html = driver.page_source#抓评论buydate = catchDate(html)#写入数据库for z in buydate:sql = "INSERT INTO ljj (id, cateid, bookid, date) VALUES (NULL, '" + rec[0] + "','" + rec[1] + "','" + z[0] + "');"try:cursor.execute(sql)except Exception, e:print econn.commit()print getTimes(nowtimes,totaltimes)driver.quit()cursor.close()conn.close() 京东图书评论有非常丰富的信息,这里面就包含了购买日期、书名、作者、好评、中评、差评等等。

以购买日期为例,使用Python + Mysql的搭配进行实现,程序不大,才100行。相关的解释我都在程序里加注了:from selenium import webdriverfrom bs4 import BeautifulSoupimport reimport win32com.clientimport threading,timeimport MySQLdbdef mydebug():driver.quit()exit(0)def catchDate(s):"""页面数据提取"""soup = BeautifulSoup(s)z = []global nowtimesm = soup.findAll("div",class_="date-buy")for obj in m:try:tmp = obj.find('br').contentsexcept Exception, e:continueif(tmp != ""):z.append(tmp)nowtimes += 1return zdef getTimes(n,t):"""获取当前进度"""return "当前进度为:" + str(int(100*n/t)) + "%"#———————————————————————————————————| 程序开始 |—————————————————————————————————#确定图书大类cate = {"3273":"历史","3279":"心理学","3276":"政治军事","3275":"国学古籍","3274":"哲学宗教","3277":"法律","3280":"文化","3281":"社会科学"}#断点续抓num1 = input("bookid:")num2 = input("pagenumber:")#生成图书大类链接,共需17355*20 = 347100次totaltimes = 347100.0nowtimes = 0#开启webdirver的PhantomJS对象#driver = webdriver.PhantomJS()driver = webdriver.Ie('C:\Python27\Scripts\IEDriverServer')#driver = webdriver.Chrome('C:\Python27\Scripts\chromedriver')#读出Mysql中的评论页面,进行抓取# 连接数据库 try:conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='jd')except Exception, e:print esys.exit()# 获取cursor对象cursor = conn.cursor()sql = "SELECT * FROM booknew ORDER BY pagenumber DESC"cursor.execute(sql)alldata = cursor.fetchall()flag = 0flag2 = 0# 如果有数据返回就循环输出,httreview/10178500-1-154.htmlif alldata:for rec in alldata:#rec[0]--bookid,rec[1]--cateid,rec[2]--pagenumberif(rec[0] != str(num1) and flag == 0):continueelse:flag = 1for p in range(num2,rec[2]):if(flag2 == 0):num2 = 0flag2 = 1p += 1link = "ht.com/review/" + rec[0] + "-1。

7.如何用Python爬取出HTML指定标签内的文本

这种情况用xpath啊,什么re和bs4都弱爆了。

import requests

from lxml import etree

def getHTMLtext(url):

res=requests.get(url,timeout=30)

# 处理中文编码问题

res.encoding='gb2312'

# 转化html

html=etree.HTML(res.text)

# xpath提取tr标签下td的内容

HTMLtext=html.xpath("//tr/td/text()")

return HTMLtext

python爬取古诗词诗句

上一篇:描写母爱的古诗诗句 下一篇:们古诗句