我需要幫助,我寫了一個小的網路搜刮器,并放入我的pythonanywhere帳戶,讓它每天運行幾次。
這里是我的代碼:
import requests
from bs4 import BeautifulSoup
import 時間
import random
list_all_results = []
for i in range(1, 3)。
time.sleep(random.uniform(1.5, 2)
print("搜刮頁面" str(i) "/745")
try:
URL = "https://www.futbin.com/players?page="/span> str(i)
平臺 = "pc"/span>
cookies = {"platform": platform}.
page = requests.get(URL, cookies=cookies)
soup = BeautifulSoup(page.content, "html.parser")
result_names = soup.find_all("a"/span>, attrs={"class"/span>: "player_name_players_table"})
result_ratings = soup.find_all(
"span",
attrs={"class": lambda r: r.startswith("form rating ut21") if r else False},
)
result_rarity = soup.find_all("td"/span>, {"class"/span>: "mobile-hide-table-col"})
result_prices_pc = soup.find_all(
"span"/span>, attrs={"class"/span>: "pc_color font-weight-bold"}。
)
list_names = []
list_ratings = []
list_rarities = []
list_prices = []
for name in result_names:
list_names.append(name.text)
for rating in result_ratings:
list_ratings.append(rating.text)
for rarity in result_rarity:
list_rarities.append(rarity.text)
for price in result_prices_pc:
n = price.text.strip()
if "K" inn:
n2 = n.replace("K"/span>, ""/span>)
full_int = int(float(n2) * 1000)
list_prices.append(full_int)
elif "M" in n:
n2 = n.replace("M"/span>, ""/span>)
full_int = int(float(n2) * 1000000)
list_prices.append(full_int)
else:
list_prices.append(int(price.text.strip())
int_list_length = len(list_names)
for i in range(0, int_list_length)。
list_all_results.append(
tuple(
(list_names[i], list_ratings[i], list_rarities[i], list_prices[i])
)
)
with open("/home/exec85/scrape/pc.txt", "a" /span>) as f: # create new .txt file and write content to file.
f.write(f"{list_all_results}")
except:
pass。
print("FINISHED"/span>)
由于某些原因,我沒有得到任何列印的結果,所以我認為沒有任何東西被搜刮,而且也沒有創建.txt檔案。 即使我手動創建.txt檔案,它也沒有被填滿。
在我的本地機器上運行腳本,一切正常。
uj5u.com熱心網友回復:
你的代碼運行良好,但如果你想讓代碼在PythonAnywhere上運行,你需要有付費帳戶,但你可以達到這個網站串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/312416.html
標籤:
上一篇:Python資料框的按行制圖
