Skip to content

Instantly share code, notes, and snippets.

@peco2282
Created August 30, 2022 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peco2282/c7e42031eca0897c91150f22f4a9fdc6 to your computer and use it in GitHub Desktop.
Save peco2282/c7e42031eca0897c91150f22f4a9fdc6 to your computer and use it in GitHub Desktop.
import time
import requests
from bs4 import BeautifulSoup, Tag
url = "https://rtrp.jp/locations/332/categories/291/?order=retrip_score&page={}"
# 記事1枚当たり15店舗載っているので定数化。
RANK = 15
def check_bs4(text: str, time: int):
print(f"---Page {time + 1}---")
soup = BeautifulSoup(text, "html.parser")
for i in range(RANK):
elem: Tag = soup.select(
f"#mainWrap > div.mainContent > section.locationSpots > div.spotList > section:nth-child({2 * i + 1}) > div.spotListContentTop > h3 > a"
)[0]
print(i + 1, "店目")
print("name : ", elem.text.strip())
print("site_url: ", elem.get("href"))
print("-------------------")
if __name__ == '__main__':
for i in range(5):
response = requests.get(url.format(i + 1))
check_bs4(response.text, i)
# 念のため2秒開ける。
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment