Пагинация Webscraping Python3- BS4 - Пока цикл - PullRequest
0 голосов
/ 30 июня 2018

Я закончил свой скребок для одной страницы и извлек href для следующей страницы.

Я не могу получить скребок в цикле для каждой последующей страницы. Я попытался выполнить цикл «Пока правда», но это убивает мои результаты с первой страницы.

Этот код отлично работает для первой страницы:

import bs4
from urllib.request import urlopen as ireq
from bs4 import BeautifulSoup as soup

myurl = ('https://www.podiuminfo.nl/concertagenda/')
uClient = ireq(myurl)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")

filename = "db.csv"
f = open(filename, "w")
headers = "Artist, Venue, City, Date\n"
f.write(headers)

DayContainer = page_soup.findAll("section",{"class":"overflow"})
print("Days on page: " + str(len(DayContainer)) + "\n")

def NextPage():
    np = page_soup.findAll("section", {"class":"next_news"})
    np = np[0].find('a').attrs['href']
    print(np)

for days in DayContainer: 
    shows = days.findAll("span", {"class":"concert_uitverkocht"})

    for soldout in shows:
        if shows:
            soldoutPlu = shows[0].parent.parent.parent

            artist = soldoutPlu.findAll("div", {"class":"td_2"})
            artist = artist[0].text.strip()

            venue = soldoutPlu.findAll("div", {"class":"td_3"})
            venue = venue[0].text

            city = soldoutPlu.findAll("div", {"class":"td_4"})
            city = city[0].text

            date = shows[0].parent.parent.parent.parent.parent
            date = date.findAll("section", {"class":"concert_agenda_date"})
            date = date[0].text
            date = date.strip().replace("\n", " ")
            print("Datum gevonden!")

            print("Artiest: " + artist)
            print("Locatie: " + venue)
            print("Stad: " + city) 
            print("Datum: " + date+ "\n")

            f.write(artist + "," + date + "," + city + "," + venue + "\n")

        else: 
            pass

NextPage()

Полагаю, нет необходимости в методе baseurl + number, потому что я могу извлечь правильный URL из каждой страницы, используя findAll. Я довольно новичок, поэтому ошибка должна быть довольно глупой.

Спасибо за помощь!

Ответы [ 2 ]

0 голосов
/ 30 июня 2018

Попробуйте приведенный ниже скрипт, чтобы получить необходимые поля, проходящие через разные страницы, и записать их соответственно в CSV-файл. Я попытался очистить ваше повторяющееся кодирование и применил немного более чистый подход вместо этого. Попробуй:

import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup

link = 'https://www.podiuminfo.nl/concertagenda/?page={}&input_plaats=&input_datum=2018-06-30&input_podium=&input_genre=&input_provincie=&sort=&input_zoek='

with open("output.csv","w",newline="",encoding="utf-8") as infile:
    writer = csv.writer(infile)
    writer.writerow(['Artist','Venue','City'])

    pagenum = -1   #make sure to get the content of the first page as well which is "0" in the link
    while True:
        pagenum+=1
        res = urlopen(link.format(pagenum)).read()
        soup = BeautifulSoup(res, "html.parser")
        container = soup.find_all("section",class_="concert_rows_info")
        if len(container)<=1:break  ##as soon as there is no content the scraper should break out of the loop

        for items in container:
            artist = items.find(class_="td_2")("a")[0].get_text(strip=True)
            venue = items.find(class_="td_3").get_text(strip=True)
            city = items.find(class_="td_4").get_text(strip=True)
            writer.writerow([artist,city,venue])
            print(f'{artist}\n{venue}\n{city}\n')
0 голосов
/ 30 июня 2018

твои ошибки

вам нужно получить URL, который вы нашли в конце вашего файла, который вы просто вызываете NextPage(), но он просто печатает URL

это была твоя ошибка :)

import bs4
from urllib.request import urlopen as ireq
from bs4 import BeautifulSoup as soup

filename = "db.csv"
#at the beginning of the document you create the file in  'w'-write mode
#but later you should open it in "A"-append mode  because 'W'-write will rewrite the file
f = open(filename, "w")
headers = "Artist, Venue, City, Date\n"
f.write(headers)
f.close()

#create a function url_fetcher that everytime will go and fetch the html
def url_fetcher(url):
    myurl = (url)
    uClient = ireq(myurl)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")
    DayContainer = page_soup.findAll("section",{"class":"overflow"})
    print("Days on page: " + str(len(DayContainer)) + "\n")
    get_artist(DayContainer, page_soup)

#here you have to call the url otherwize it wont work
def NextPage(page_soup):
    np = page_soup.findAll("section", {"class":"next_news"})
    np = np[0].find('a').attrs['href']
    url_fetcher(np)

#in get artist you have some repeatings but you can tweak alittle bit and it will work
def get_artist(DayContainer, page_soup):
    for days in DayContainer:
        shows = days.findAll("span", {"class":"concert_uitverkocht"})

        for soldout in shows:
            print(soldout)
            if shows:
                soldoutPlu = shows[0].parent.parent.parent

                artist = soldoutPlu.findAll("div", {"class":"td_2"})
                artist = artist[0].text.strip()

                venue = soldoutPlu.findAll("div", {"class":"td_3"})
                venue = venue[0].text

                city = soldoutPlu.findAll("div", {"class":"td_4"})
                city = city[0].text

                date = shows[0].parent.parent.parent.parent.parent
                date = date.findAll("section", {"class":"concert_agenda_date"})
                date = date[0].text
                date = date.strip().replace("\n", " ")
                print("Datum gevonden!")

                print("Artiest: " + artist)
                print("Locatie: " + venue)
                print("Stad: " + city)
                print("Datum: " + date+ "\n")
                with open(filename, "a") as f:
                    f.write(artist + "," + date + "," + city + "," + venue + "\n")

            else:
                pass
        NextPage(page_soup)
url_fetcher('https://www.podiuminfo.nl/concertagenda/')

1009 * резюмировать * для облегчения понимания я сделал большой цикл, но он работает :) вам нужно сделать некоторые настройки, чтобы в db.csv не было повторяющихся имен и дат.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...