用于循环列表格式功能

时间:2018-12-19 15:21:40

标签: python python-3.x beautifulsoup python-requests

我正在研究使用格式功能来解析for循环中的URL列表,并为每个URL运行多个命令。

以下代码仅给我四个用户中的一个一个。如何遍历每个用户? 在每个网址上实现 +1

from bs4 import BeautifulSoup
import requests

user_list = {'username':'social_media_username', 'username':'social_media_username' , 'username':'social_media_username' , 'username':'social_media_username'}
url_pattern = 'https://hypeauditor.com/report/{username}'

for user in zip(user_list):
    url = url_pattern.format(**user_list)
    r = requests.get(url)
    response = r.content.decode('utf-8')
    each_soup = BeautifulSoup(response, 'html.parser')

    for url in each_soup:
        get__data = each_soup.findAll(class_ = 'kyb-user-info-v2__el kyb-user-info-v2__el--followers')[1].find(class_='kyb-user-info-v2__sub-title').text
        print(get__data)

2 个答案:

答案 0 :(得分:0)

l更改为用户列表,并使用它来获取关注者详细信息。假设追随者属于此类 kyb-user-info-v2__el kyb-user-info-v2__el--关注者

from bs4 import BeautifulSoup
import requests

l = [{'username':'social_media_username'}, {'username':'social_media_username'} , {'username':'social_media_username' },{'username':'social_media_username'}]
url_patter = 'https://hypeauditor.com/report/{username}'

for user in l:
    url = url_patter.format(**user)
    r = requests.get(url)
    response = r.content.decode('utf-8')
    each_soup =BeautifulSoup(response, 'html.parser')
    followers=each_soup.findAll(class_ = 'kyb-user-info-v2__el kyb-user-info-v2__el--followers')
    for follower in followers:
        # Get follower information
        get__data = follower.find(class_='kyb-user-info-v2__sub-title').text
        print(get__data)

答案 1 :(得分:0)

您的“ user_list”不是列表,而是命令。字典键是唯一的,因此,由于您对所有用户使用相同的键(“用户名”),因此该字典中实际上仅存储一个值,即:

>>> d = {"username":"foo", "username":"bar", "username":"baaz"}
>>> d
{'username': 'baaz'}

此外,将zip()应用于字典仅会选择字典键:

>>> list(zip(d))
[('username',)]

解决方案是使user_list成为适当的列表-字典列表:

>>> users = [{"username":"foo"}, {"username":"bar"}, {"username":"baaz"}]
>>> for user in users:
...     print(pattern.format(**user))

或更简单(由于每个字典中只有一个值)列出了用户名字符串:

>>> usernames = ["foo", "bar", "baaz"]
>>> for username in usernames:
...     print(pattern.format(username=username))