import requests
from lxml import etree
from bs4 import BeautifulSoup
import os
resp = requests.get('http://www.748219.com/')
resp.encoding = 'utf-8' #编码方式
#print(resp.text)
book = resp.text
html = etree.HTML(book)
#提取详情页链接
book_url = html.xpath('//div[@class="item"]/ul/li/a/@href')
#提取名称
book_name = html.xpath('//div[@class="item"]/ul/li/a/text()')
#print(book_url,book_name)
#将名称与链接一一对应
book_data = list(zip(book_name,book_url))
#print(book_data)
print('准备提取')
#提取文章
if not os.path.exists("美文"):
os.mkdir("美文")
for name,url in book_data:
#print(name,url)
#获取源代码
response = requests.get(url)
response.encoding = 'utf-8'
#提取文本
soup = BeautifulSoup(response.text,'lxml')#源代码,lxml解析HTML代码
test = soup.find('div',id='zoomtext').text.strip()
print(test)
path = './美文/'
print('准备打印')
with open(path + name + ".txt", "a", encoding="utf-8") as f:
f.write(test)
print("正在保存--%s" % name)
#将数据保存到电脑
爬取文章
最新推荐文章于 2022-01-14 09:54:49 发布