pycharm怎么爬取视频章节标题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pycharm怎么爬取视频章节标题相关的知识,希望对你有一定的参考价值。
参考技术A pycharm爬取数据可以通过printf在控制台输出,也可以将其写入数据库或文件做输出。利用bs4爬取三国演义所有章节标题以及章节内容
url = ‘ http://www.shicimingju.com/book/sanguoyanyi.html‘
from bs4 import BeautifulSoup import requests url = ‘http://www.shicimingju.com/book/sanguoyanyi.html‘ headers = { ‘User-Agent‘: ‘Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36‘ } page_text = requests.get(url=url,headers=headers).text soup = BeautifulSoup(page_text,‘lxml‘) res_list = soup.select(‘.book-mulu a‘) with open(‘三国演义.text‘,‘w‘,encoding=‘utf-8‘)as f: for item in res_list: url_item = ‘%s%s‘%("http://www.shicimingju.com",item[‘href‘]) detail_page_text = requests.get(url=url_item, headers=headers).text detail_soup = BeautifulSoup(detail_page_text,‘lxml‘) title = detail_soup.find(‘div‘,class_=‘www-main-container‘).text body = detail_soup.find("div",class_=‘chapter_content‘).text f.write(title+‘ ‘+body)
以上是关于pycharm怎么爬取视频章节标题的主要内容,如果未能解决你的问题,请参考以下文章