Это потому, что вы сначала перебираете URL-адреса и извлекаете содержимое для каждого в одной и той же переменной page
здесь:
for url in temp:
page = requests.get(url)
, поэтому страница всегда будет содержать последнюю страницу URL-адреса, чтобы решить эту проблему вам нужно обработать страницу после извлечения
followers_list = []
for url in temp:
page = requests.get(url)
bs = BeautifulSoup(page.text, "html.parser")
follow_box = bs.find('li',{'class':'ProfileNav-item ProfileNav-item--followers'})
followers = follow_box.find('a').find('span',{'class':'ProfileNav-value'})
print("Number of followers: {} ".format(followers.get('data-count')))
followers_list.append(followers.get('data-count'))
print(followers_list)
вот полный пример для проверки
from bs4 import BeautifulSoup
import requests
import pandas as pd
purcsv = pd.read_csv('pureeng.csv')
followers = purcsv['username']
handles = purcsv['username'][0:40].tolist()
followers_list = []
for handle in handles:
url = "https://twitter.com/" + handle
try:
page = requests.get(url)
except Exception as e:
print(f"Failed to fetch page for url {url} due to: {e}")
continue
bs = BeautifulSoup(page.text, "html.parser")
follow_box = bs.find('li',{'class':'ProfileNav-item ProfileNav-item--followers'})
followers = follow_box.find('a').find('span',{'class':'ProfileNav-value'})
print("Number of followers: {} ".format(followers.get('data-count')))
followers_list.append(followers.get('data-count'))
print(followers_list)
вывода:
Number of followers: 13714085
Number of followers: 4706511
['13714085', '4706511']
Вы можете рассмотреть возможность использования async
Функция для извлечения и обработки этих URL-адресов, если у вас есть два из них.