爬《斗破苍穹》小说

软件pycharm

实现下载request,lxml,BeautifulSoup这些包
pip install lxml
pip install BeautifulSoup
pip install request
UA伪装
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
然后用BeautifulSoup定位到标签


全部代码如下:
from time import sleep

import requests
import lxml
from bs4 import BeautifulSoup

url = 'https://www.ibswtan.com/0/425/'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Host": "www.ibswtan.com",
    "Referer": "https://www.ibswtan.com/0/425/"
}
resp = requests.get(url=url, headers=headers)
resp.encoding = 'utf-8'
page_text = resp.text
soup = BeautifulSoup(page_text, 'lxml')
list_title = soup.select('#list > dl > dd')
fp = open("斗破苍穹.txt", 'w', encoding='utf-8')
for list in list_title:
    title = list.a.string
    title_links = list.a['href']
    title_url = "https://www.ibswtan.com/0/425/" + title_links

    sleep(1)

    list_resp = requests.get(url=title_url, headers=headers)
    list_resp.encoding = 'utf-8'
    list_page_text = list_resp.text
   
    list_soup = BeautifulSoup(list_page_text, 'lxml')
    list_tag_id = list_soup.find('div', id='content')
    neirong = list_tag_id.text
  
    fp.write(title +"\n"+ neirong + "\n")
    print(title + "    " + "爬取完成")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值