私はこれにはかなり新しく、スクラップの仕方を学ぶためのスクリプトを構築しています。 私は、必要な連絡先情報を含むURLのリストのメインインデックスページを照会しています。最初に実行した後のループスルーセットの停止 - Python
インデックスリストをセットに入れてから、2つの関数を使ってインデックスリストを反復しようとしました(これを行うより良い方法があると確信しています)。最初の反復の後、それは停止し、私は単に理由を理解していません。任意のポインタが高く評価されました。
import requests
from bs4 import BeautifulSoup
linkset = set()
url = "http://someurl.com/venues"
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")
base_url = "http://someurl.com/uk/"
links = soup.find_all("a", class_="supplier-link")
# A function to get the links from the top level directory.
def get_venue_link_list(links):
for link in links:
linkset.add(link.get("href"))
return linkset
#get_venue_link_list(links)
# When I test by printing linkset, I get the list of unique URL's.
# This works as expected.
#print linkset
# A function to go retrieve contact
def go_retrieve_contact(link_value):
for i in link_value:
link = i
venue_link = base_url + link
venue_request = requests.get(venue_link)
venue_soup = BeautifulSoup(venue_request.content, "lxml")
info = venue_soup.find_all("section", {"class": "findout"})
header = venue_soup.find_all("div", {"id": "supplier-header-desktop"})
go_get_info(info)
# Email, Phone and Website was nested in one div so it was a little easier to get.
# Will need to use a different div for address and social media names.
def go_get_info(info):
for item in info:
print "%s" % ((item.contents[3].find_all("span", {"class": "text"})[0].text)).strip()
print "%s" % ((item.contents[3].find_all("span", {"class": "text"})[1].text)).strip()
print "%s" % ((item.contents[3].find_all("span", {"class": "text"})[2].text)).strip()
#Lets comment out this next nested loop until I fix the above.
#for item in header:
#print item.contents[1].text
go_retrieve_contact(get_venue_link_list(links))