无法使用 getattr() 函数解析 bs4 src 属性
Can't parse bs4 src attribute using the getattr() function
我创建了一个脚本来解析网页中每个电影容器的两个字段。脚本运行良好。
我正在尝试使用此 getattr()
函数从两个字段中抓取文本和 src,如 movie_name
和 image_link
。在 movie_name
的情况下,它有效。但是,当我尝试解析 image_link
.
时失败了
目前有一个功能被注释掉了,当我取消注释时它会起作用。但是,我的目标是利用 getattr()
来解析 src
.
import requests
from bs4 import BeautifulSoup
url = "https://yts.am/browse-movies"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
# def get_information(url):
# res = requests.get(url,headers=headers)
# soup = BeautifulSoup(res.text,'lxml')
# for row in soup.select(".browse-movie-wrap"):
# movie_name = row.select_one("a.browse-movie-title").text
# image_link = row.select_one("img.img-responsive").get("src")
# yield movie_name,image_link
def get_information(url):
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.text,'lxml')
for row in soup.select(".browse-movie-wrap"):
movie_name = getattr(row.select_one("a.browse-movie-title"),"text",None)
image_link = getattr(row.select_one("img.img-responsive"),"src",None)
yield movie_name,image_link
if __name__ == '__main__':
for items in get_information(url):
print(items)
How can I scrape src
using getattr()
function?
这样做的原因:
movie_name = getattr(row.select_one("a.browse-movie-title"),"text",None)
但事实并非如此:
image_link = getattr(row.select_one("img.img-responsive"),"src",None)
是因为class的方法也是属性。因此,实际上,您正在做的是为第一个示例获取 function text。换句话说,没有名为 src
.
的方法或属性
如果您查看 attributes
个:
row.select_one("a.browse-movie-title").attrs
您将获得:
{'href': 'https://yts.mx/movies/imperial-blue-2019', 'class': ['browse-movie-title']}
同样,
row.select_one(".img-responsive").attrs
输出为:
{'class': ['img-responsive'], 'src': 'https://img.yts.mx/assets/images/movies/imperial_blue_2019/medium-cover.jpg', 'alt': 'Imperial Blue (2019) download', 'width': '170', 'height': '255'}
所以,如果我们试验并这样做:
getattr(row.select_one(".img-responsive"), "attrs", None).src
我们最终会得到:
AttributeError: 'dict' object has no attribute 'src'
因此,如评论中所述,这不是您在 bs4
对象上以纯 Python 的方式使用 getattr()
的方式。您可以使用 .get()
方法或 [key]
语法。
例如:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
def get_information(url):
soup = BeautifulSoup(requests.get(url, headers=headers).text, 'lxml')
for row in soup.select(".browse-movie-wrap"):
movie_name = row.select_one("a.browse-movie-title").getText()
image_link = row.select_one("img.img-responsive").get("src")
yield movie_name, image_link
if __name__ == '__main__':
for items in get_information("https://yts.am/browse-movies"):
print(items)
这会产生:
('Imperial Blue', 'https://img.yts.mx/assets/images/movies/imperial_blue_2019/medium-cover.jpg')
('Ablaze', 'https://img.yts.mx/assets/images/movies/ablaze_2001/medium-cover.jpg')
('[CN] Long feng zhi duo xing', 'https://img.yts.mx/assets/images/movies/long_feng_zhi_duo_xing_1984/medium-cover.jpg')
('Bobbie Jo and the Outlaw', 'https://img.yts.mx/assets/images/movies/bobbie_jo_and_the_outlaw_1976/medium-cover.jpg')
('Adam Resurrected', 'https://img.yts.mx/assets/images/movies/adam_resurrected_2008/medium-cover.jpg')
('[ZH] The Wasted Times', 'https://img.yts.mx/assets/images/movies/the_wasted_times_2016/medium-cover.jpg')
('Promise', 'https://img.yts.mx/assets/images/movies/promise_2021/medium-cover.jpg')
and so on ...
最后,如果你真的想用getattr()
解析这个,你可以试试这个:
movie_name = getattr(row.select_one("a.browse-movie-title"), "getText", None)()
image_link = getattr(row.select_one("img.img-responsive"), "attrs", None)["src"]
你仍然会得到相同的结果,但是,恕我直言,这比普通的 .getText()
和 .get("src")
语法太复杂而且可读性也不太好。
我创建了一个脚本来解析网页中每个电影容器的两个字段。脚本运行良好。
我正在尝试使用此 getattr()
函数从两个字段中抓取文本和 src,如 movie_name
和 image_link
。在 movie_name
的情况下,它有效。但是,当我尝试解析 image_link
.
目前有一个功能被注释掉了,当我取消注释时它会起作用。但是,我的目标是利用 getattr()
来解析 src
.
import requests
from bs4 import BeautifulSoup
url = "https://yts.am/browse-movies"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
# def get_information(url):
# res = requests.get(url,headers=headers)
# soup = BeautifulSoup(res.text,'lxml')
# for row in soup.select(".browse-movie-wrap"):
# movie_name = row.select_one("a.browse-movie-title").text
# image_link = row.select_one("img.img-responsive").get("src")
# yield movie_name,image_link
def get_information(url):
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.text,'lxml')
for row in soup.select(".browse-movie-wrap"):
movie_name = getattr(row.select_one("a.browse-movie-title"),"text",None)
image_link = getattr(row.select_one("img.img-responsive"),"src",None)
yield movie_name,image_link
if __name__ == '__main__':
for items in get_information(url):
print(items)
How can I scrape
src
usinggetattr()
function?
这样做的原因:
movie_name = getattr(row.select_one("a.browse-movie-title"),"text",None)
但事实并非如此:
image_link = getattr(row.select_one("img.img-responsive"),"src",None)
是因为class的方法也是属性。因此,实际上,您正在做的是为第一个示例获取 function text。换句话说,没有名为 src
.
如果您查看 attributes
个:
row.select_one("a.browse-movie-title").attrs
您将获得:
{'href': 'https://yts.mx/movies/imperial-blue-2019', 'class': ['browse-movie-title']}
同样,
row.select_one(".img-responsive").attrs
输出为:
{'class': ['img-responsive'], 'src': 'https://img.yts.mx/assets/images/movies/imperial_blue_2019/medium-cover.jpg', 'alt': 'Imperial Blue (2019) download', 'width': '170', 'height': '255'}
所以,如果我们试验并这样做:
getattr(row.select_one(".img-responsive"), "attrs", None).src
我们最终会得到:
AttributeError: 'dict' object has no attribute 'src'
因此,如评论中所述,这不是您在 bs4
对象上以纯 Python 的方式使用 getattr()
的方式。您可以使用 .get()
方法或 [key]
语法。
例如:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}
def get_information(url):
soup = BeautifulSoup(requests.get(url, headers=headers).text, 'lxml')
for row in soup.select(".browse-movie-wrap"):
movie_name = row.select_one("a.browse-movie-title").getText()
image_link = row.select_one("img.img-responsive").get("src")
yield movie_name, image_link
if __name__ == '__main__':
for items in get_information("https://yts.am/browse-movies"):
print(items)
这会产生:
('Imperial Blue', 'https://img.yts.mx/assets/images/movies/imperial_blue_2019/medium-cover.jpg')
('Ablaze', 'https://img.yts.mx/assets/images/movies/ablaze_2001/medium-cover.jpg')
('[CN] Long feng zhi duo xing', 'https://img.yts.mx/assets/images/movies/long_feng_zhi_duo_xing_1984/medium-cover.jpg')
('Bobbie Jo and the Outlaw', 'https://img.yts.mx/assets/images/movies/bobbie_jo_and_the_outlaw_1976/medium-cover.jpg')
('Adam Resurrected', 'https://img.yts.mx/assets/images/movies/adam_resurrected_2008/medium-cover.jpg')
('[ZH] The Wasted Times', 'https://img.yts.mx/assets/images/movies/the_wasted_times_2016/medium-cover.jpg')
('Promise', 'https://img.yts.mx/assets/images/movies/promise_2021/medium-cover.jpg')
and so on ...
最后,如果你真的想用getattr()
解析这个,你可以试试这个:
movie_name = getattr(row.select_one("a.browse-movie-title"), "getText", None)()
image_link = getattr(row.select_one("img.img-responsive"), "attrs", None)["src"]
你仍然会得到相同的结果,但是,恕我直言,这比普通的 .getText()
和 .get("src")
语法太复杂而且可读性也不太好。