删去下列代码中info1_list的html标签import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt import numpy as np headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.41'} url='https://www.ibiquges.com/xiaoshuodaquan/' strhtml=requests.get(url,headers=headers) soup=BeautifulSoup(strhtml.text,'lxml') info_list = []#书籍目录 count_list = []#书籍数量 info1_list = [] for i in range(1, 14, 2): info = soup.select(f'#main > div:nth-child({i}) > ul') info1 = soup.select(f'#main > div:nth-child({i}) > h2') for item in info: #print(item.get_text()) count = len(info[0].find_all('li')) count_list.append(count) info_list.append(info) for item1 in info1: print(item1.get_text()) info1_list.append(info1) print(info1_list) print(count_list)
时间: 2024-04-03 14:36:25 浏览: 22
可以使用BeautifulSoup库中的get_text()方法来删除html标签。修改代码如下:
```python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import numpy as np
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.41'}
url = 'https://www.ibiquges.com/xiaoshuodaquan/'
strhtml = requests.get(url, headers=headers)
soup = BeautifulSoup(strhtml.text, 'lxml')
info_list = [] # 书籍目录
count_list = [] # 书籍数量
info1_list = []
for i in range(1, 14, 2):
info = soup.select(f'#main > div:nth-child({i}) > ul')
info1 = soup.select(f'#main > div:nth-child({i}) > h2')
for item in info:
# print(item.get_text())
count = len(info[0].find_all('li'))
count_list.append(count)
info_list.append(item.get_text()) # 使用get_text()方法删除html标签
for item1 in info1:
print(item1.get_text())
info1_list.append(item1.get_text()) # 使用get_text()方法删除html标签
print(info1_list)
print(count_list)
```
这样就可以将info1_list中的html标签删除了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
import requests import re # from bs4 import BeautifulSoup import matplotlib.pyplot as plt import numpy as np # import pandas as pd i = 1 lists = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250] title = [] year = [] country = [] score = [] number = [] for page in range(0, 226, 25): url = 'https://movie.douban.com/top250?start=' + str(page) + '&filter=' headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"} resp = requests.get(url=url, headers=headers) resp.encoding = "utf-8" pattern = re.compile( r'.*? < img width="100" alt="(?P<title>.*?)".*?class="">.*?.*?导演: (?P<director>.*?) .*?
.*?(?P<year>.*?) / (?P<country>.*?) .*?"v:average">(?P<score>.*?).*?(?P<number>.*?)人评价', re.S) pic_url = re.compile(r'< img width="100".*?src="(.*?)" class="">', re.S) pic_URl = pic_url.findall(resp.text) data2 = pattern.finditer(str(resp.text)) for url1 in pic_URl: file1 = open('films.pic\\' + str(i) + '.jpg', 'ab') Pic = requests.get(url1) file1.write(Pic.content) i = i + 1 file1.close() file2 = open('movie.text', 'a+', encoding='utf-8') for m in data2: if int(m['number']) / 100000 > 13: number.append(int(m['number']) / 100000) country.append(m['country']) year.append(m['year']) title.append(m['title']) score.append(m['score']) file2.write( '电影名:' + m['title'] + ', 导演:' + m['director'] + ', 年份:' + m['year'] + ', 国家:' + m['country'] + ', 评分:' + m[ 'score'] + ',评价人数:' + str(int(m['number']) / 100000) + ' 100k') file2.write('\n') print( '电影名:' + m['title'] + ', 导演:' + m['director'] + ', 年份:' + m['year'] + ', 国家:' + m['country'] + ', 评分:' + m[ 'score'] + ',评价人数:' + str(int(m['number']) / 100000) + ' 100k')
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)