语句soup.find_all('ul',attrs={'class':'list1'})和语句soup.find_all('ul',class_='list1')作用是一样的。
时间: 2023-10-07 21:05:44 浏览: 47
是的,这两个语句的作用是一样的,都是通过 BeautifulSoup 库中的 `find_all()` 方法查找 HTML 文档中所有 `ul` 标签中 `class` 属性值为 `list1` 的元素并返回。只不过第一个语句使用了 `attrs` 参数指定属性及其值,而第二个语句使用了 `class_` 参数来指定 `class` 属性值。这是因为在 Python 中 `class` 是关键字,所以在使用 BeautifulSoup 库时需要使用 `class_` 来指定 `class` 属性值。
相关问题
for tag in soup.find_all(attrs={"class": "item"}): # 爬取序号 num = tag.find('em').get_text() print(num) infofile.write(num + "\r\n") # 电影名称 name = tag.find_all(attrs={"class": "title"}) zwname = name[0]
这段代码是用来爬取网页中的电影序号和名称。首先,通过`soup.find_all`方法找到所有具有`class`属性为"item"的标签。然后,通过`tag.find('em').get_text()`获取序号,并将其打印出来和写入到文件中。接下来,通过`tag.find_all(attrs={"class": "title"})`找到所有具有`class`属性为"title"的标签,并将第一个标签的文本内容赋值给变量`zwname`。
import requestsfrom bs4 import BeautifulSoup# 构造请求urlurl = 'https://www.tianqi.com/zhongmou/10/'# 发送GET请求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.43'}response = requests.get(url,headers=headers)# 判断请求是否成功if response.status_code == 200: # 解析HTML页面 soup = BeautifulSoup(response.text, 'html.parser') # 获取天气信息 city = soup.find_all('ul class_="weaul"', class_='crumbs fl').find_all('a')[-1].text.strip() weather = soup.find_all('ul class_="weaul"', class_='wea').text.strip() temperature = soup.fin_alld('ul class_="weaul"', class_='tem').span.text.strip() wind = soup.find_all('ul class_="weaul"', class_='').i.text.strip() humidity = soup.find_all('ul class_="weaul"', class_='humidity').span.text.strip() # 将天气信息写入文本文件 with open('weather.txt', 'w', encoding='utf-8') as f: f.write(f"城市:{city}\n天气:{weather}\n温度:{temperature}\n风力:{wind}\n湿度:{humidity}")else: print("请求天气预报数据失败!")检查并修改以上程序
在程序中,以下代码存在错误:
```
city = soup.find_all('ul class_="weaul"', class_='crumbs fl').find_all('a')[-1].text.strip()
weather = soup.find_all('ul class_="weaul"', class_='wea').text.strip()
temperature = soup.fin_alld('ul class_="weaul"', class_='tem').span.text.strip()
wind = soup.find_all('ul class_="weaul"', class_='').i.text.strip()
humidity = soup.find_all('ul class_="weaul"', class_='humidity').span.text.strip()
```
应将上述代码修改为:
```
city = soup.find('div', class_='crumbs fl').find_all('a')[-1].text.strip()
weather = soup.find('ul', class_='weaul').find('p', class_='wea').text.strip()
temperature = soup.find('ul', class_='weaul').find('p', class_='tem').span.text.strip()
wind = soup.find('ul', class_='weaul').find('p', class_='win').i.text.strip()
humidity = soup.find('ul', class_='weaul').find('p', class_='hum').span.text.strip()
```
修改后的代码如下:
```
import requests
from bs4 import BeautifulSoup
# 构造请求url
url = 'https://www.tianqi.com/zhongmou/10/'
# 发送GET请求
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.43'}
response = requests.get(url,headers=headers)
# 判断请求是否成功
if response.status_code == 200:
# 解析HTML页面
soup = BeautifulSoup(response.text, 'html.parser')
# 获取天气信息
city = soup.find('div', class_='crumbs fl').find_all('a')[-1].text.strip()
weather = soup.find('ul', class_='weaul').find('p', class_='wea').text.strip()
temperature = soup.find('ul', class_='weaul').find('p', class_='tem').span.text.strip()
wind = soup.find('ul', class_='weaul').find('p', class_='win').i.text.strip()
humidity = soup.find('ul', class_='weaul').find('p', class_='hum').span.text.strip()
# 将天气信息写入文本文件
with open('weather.txt', 'w', encoding='utf-8') as f:
f.write(f"城市:{city}\n天气:{weather}\n温度:{temperature}\n风力:{wind}\n湿度:{humidity}")
else:
print("请求天气预报数据失败!")
```
这样就可以正确的获取天气信息并将其写入文本文件中了。