小白第一次独立写完代码,嘻嘻,爬诗词
不多比比,直接发源码,嘻嘻,小白一起交流学习,大佬多多给点建议和指导,谢谢!!!@.@# 作者:Administrator
# 日期:2019/7/26 23:38
# 工具:PyCharm
import requests
from lxml import etree
from pool import headers
def get_url():
header = headers()
page = int(input('请输入要爬取的页数:'))
for j in range(1,page):
try:
url = 'http://www.shicimingju.com/chaxun/zuozhe/13046_'+str(j)+'.html'
response = requests.get(url,headers=header)
html = etree.HTML(response.content.decode())
new_url = html.xpath('//div[@class="www-shadow-card www-main-container"]/h3/a/@href')
for i in new_url:
get_datas(i)
except Exception as err:
print(err)
def get_datas(i):
header = headers()
url = 'http://www.shicimingju.com'+i
response = requests.get(url, headers=header)
html = etree.HTML(response.content.decode('utf-8'))
title = ''.join(html.xpath('//div[@class="shici-container www-shadow-card"]/h1/text()'))
content = '\n'.join(html.xpath('//div[@class="shici-content"]/text()')).strip()
save_datas(title,content)
def save_datas(title,content):
print('正在下载的诗词名字是:%s'%title)
with open('gushi.txt','a',encoding='utf-8') as f:
f.write(' 《'+title+'》'+'\n')
f.write(content+'\n')
f.write('-'*60+'\n')
if __name__ == '__main__':
get_url()
那个啥,浏览器代{过}{滤}理池,小白可以看一下
import random
def headers():
user_agent = [
"{'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'}",
"{'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)'}",
"{'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'}",
"{'User-Agent':'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)'}",
"{'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)'}",
"{'User-Agent':'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'}",
"{'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)'}",
"{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)'}",
"{'User-Agent':'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6'}",
"{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1'}",
"{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0'}",
"{'User-Agent':'Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5'}",
"{'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6'}",
"{'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'}",
"{'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20'}",
"{'User-Agent':'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52'}",
]
header = random.choice(user_agent)
return header 雷晨 发表于 2019-11-4 20:03
楼主你好,请问可以出个爬http://mzsock.com/这个网站的教程吗?谢谢了
明天试试看 不拍电影不掉泪 发表于 2019-7-27 11:33
真“好色”
哈哈,下次爬个有色电影网站,看你到时候怎么说 连诗词都不放过,真“好学”
{:301_1001:} 真“好色” 感谢楼主分享,学习了 可以的,下次准备爬什么 我还爬过词典😂 卖人的小行家 发表于 2019-7-27 12:28
可以的,下次准备爬什么
嘻嘻 爬图片 hyy1000 发表于 2019-7-27 12:29
我还爬过词典😂
我也去试试:lol 谢谢,学习了
页:
[1]
2