Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 刮取时调用同一网站的字典循环_Python_Loops_Dictionary_Beautifulsoup_Screen Scraping - Fatal编程技术网

Python 刮取时调用同一网站的字典循环

Python 刮取时调用同一网站的字典循环,python,loops,dictionary,beautifulsoup,screen-scraping,Python,Loops,Dictionary,Beautifulsoup,Screen Scraping,我对Python非常陌生,所以这可能很简单,并且可能是一个缩进问题。我正在尝试使用BeautifulSoup浏览几个网页,创建一个字典列表,之后我可以使用它来处理数据 代码似乎工作得很好,但我最后得到的列表(liste_flat)只是相同两个字典的列表。我想要一份不同字典的清单 def scrap_post(url): url = "https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDY

我对Python非常陌生,所以这可能很简单,并且可能是一个缩进问题。我正在尝试使用BeautifulSoup浏览几个网页,创建一个字典列表,之后我可以使用它来处理数据

代码似乎工作得很好,但我最后得到的列表(liste_flat)只是相同两个字典的列表。我想要一份不同字典的清单

def scrap_post(url):
    url = "https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/".format(i)
    dictionary = {}
    response = requests.get(url)
    soup = bs(response.text,"lxml")
    taille = len(soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")) #48 entries
    for num_ville in range(0,taille):
        print(num_ville)
        apt_id = soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")[num_ville]['data-id']
        entry = soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")[num_ville]
        pricepw = soup.find_all('div', class_='col-xs-5 col-sm-4 price')[num_ville].find('h3').text.encode('utf-8').replace('\xc2\xa3','',).replace('pw','',).strip()
        rooms = soup.find_all('div', class_='col-xs-6 type')[num_ville].find('p').text.encode('utf-8').strip()
        lat = soup.find_all('div', {"itemprop":"geo"})[num_ville].find('meta', {'itemprop':'latitude'})['content']
        lon = soup.find_all('div', {"itemprop":"geo"})[num_ville].find('meta', {'itemprop':'longitude'})['content']
        dictionary[num_ville]={'Price per week':pricepw,'Rooms':rooms,'Latitude':lat,'Longitude':lon}
    return dictionary

#get all URLs
liste_url = []
liste_url = ['https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/''%i' %i for i in range(1,3)]

#get flats
liste_flat = [scrap_post(i) for i in liste_url] 
我一定是在同一个网站上循环了两次。有没有关于如何确保我在不同的网站上循环的建议


谢谢

是的,您正在同一个网站上循环,因为您已经在函数中硬编码了
url
变量

url = "https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/".format(i)

这意味着无论您向函数发送什么,它都将始终使用此url。你可能想删除它。您还没有在字符串中放置占位符,
.format(i)
实际上没有任何作用。

谢谢,这正是问题所在!我只是拿出了你推荐的第一个url,它工作得很好。