import requests from bs4 import BeautifulSoup import lxml headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel)" } for start_name in range(0, 250, 25): res = requests.get(f"https://book.douban.com/top250?start=", headers=headers) print(res.status_code) soup = BeautifulSoup(res.text, 'lxml') t1 = soup.findAll('div', attrs={'class': 'pl2'}) for i in t1: t2 = i.find('a').text.strip() # 去除<a>标签中的空格 print(t2);删除':'前的空白
时间: 2023-06-19 18:02:55 浏览: 36
import requests
from bs4 import BeautifulSoup
import lxml
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel)"
}
for start_name in range(0, 250, 25):
res = requests.get(f"https://book.douban.com/top250?start={start_name}", headers=headers) #修改url拼接方式
print(res.status_code)
soup = BeautifulSoup(res.text, 'lxml')
t1 = soup.findAll('div', attrs={'class': 'pl2'})
for i in t1:
t2 = i.find('a').text.strip().lstrip(':') #去除冒号前的空白
print(t2)
相关问题
import requests from bs4 import BeautifulSoup import lxml headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel)" } for start_name in range(0, 250, 25): res = requests.get(f"https://book.douban.com/top250?start=", headers=headers) print(res.status_code) soup = BeautifulSoup(res.text, 'lxml') t1 = soup.findAll('div', attrs={'class': 'pl2'}) for i in t1: t2 = i.find('a').text.strip() # 去除<a>标签中的空格 print(t2);去除其中会出现的空格
代码中的错误是在发送请求时没有正确地拼接 URL。应该将起始页数 `start_name` 与 URL 拼接在一起,修改如下:
```python
import requests
from bs4 import BeautifulSoup
import lxml
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel)"
}
for start_name in range(0, 250, 25):
url = f"https://book.douban.com/top250?start={start_name}"
res = requests.get(url, headers=headers)
print(res.status_code)
soup = BeautifulSoup(res.text, 'lxml')
t1 = soup.findAll('div', attrs={'class': 'pl2'})
for i in t1:
t2 = i.find('a').text.strip() # 去除<a>标签中的空格
print(t2)
```
下列代码中统计id为 "main" 的元素的第一个子元素 ul下的元素个数import requests from bs4 import BeautifulSoup 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=soup.select('#main > div:nth-child(1) > ul') for item in info: print(item.get_text())
可以使用 Python 的内置函数 len() 统计 id 为 "main" 的元素的第一个子元素 ul 下的元素个数。代码如下:
```
import requests
from bs4 import BeautifulSoup
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 = soup.select('#main > div:nth-child(1) > ul')
count = len(info[0].find_all('li'))
print(count)
```
首先,我们使用 len() 函数统计了 info 列表中第一个元素中的 li 元素的个数,并将结果赋值给变量 count。其中,find_all() 方法返回一个列表,其中包含所有匹配的元素。接着使用 print() 函数打印结果即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)