import requests from bs4 import BeautifulSoup import csv def main(): pattern = 'https://forum.dirt.ru/member.php?u={}' for i in range(0, 76128): url = pattern.format(str(i)) get_html(url) def get_html(url): rs = requests.get(url) root = BeautifulSoup(rs.content, 'html.parser') nickname = root.select_one('.bigusername').text.strip() data = { 'nickname': nickname} write_csv(data) def write_csv(data): with open('nickname.csv', 'a') as f: writer = csv.writer(f) writer.writerow((data['nickname'])) if __name__ == '__main__': main() 

Why does it return null and not create a csv file?

  • one
    main cause? you have in def get_html(): error - no url parameter. Is this the code that you use or when composing the question sealed? - gil9red
  • one
    Hmm, it's not clear what's going on with you. You see what's the matter, in the question of one code, you have another. And you have a problem with both. Put your code in question ( править button) Checked what is in data ? - gil9red
  • one
    Add at the end: if __name__ == '__main__': main() , otherwise your code is not executed - gil9red
  • one
    Doom! Current error due to root.select_one('.bigusername'). returned None, i.e. That nickname was not found at boot. Vanguyu that, in his own cycle, you staged a dos attack on the site and at some iteration he returned the stub, for example with a captcha - gil9red
  • one
    because it is not necessary for .text to write brackets - this is not a function - gil9red

1 answer 1

Slightly modified the code and set a limit on the cycle:

 import requests from bs4 import BeautifulSoup import csv def write_csv(data): with open('nickname.csv', 'a', newline='') as f: writer = csv.writer(f) # Список на 1 элемент row = [data['nickname']] writer.writerow(row) def get_html(url): rs = requests.get(url) root = BeautifulSoup(rs.content, 'html.parser') nickname = root.select_one('.bigusername').text.strip() data = {'nickname': nickname} write_csv(data) def main(): pattern = 'https://forum.dirt.ru/member.php?u={}' for i in range(3): url = pattern.format(i + 1) get_html(url) if __name__ == '__main__': main() 

Result (nickname.csv):

 Dronix testing_vbulletin kolyanich