爬行输出 - 连接两个变量

时间:2015-11-22 15:06:28

标签: python beautifulsoup web-crawler

我想知道如何组合两个变量。

我的目标是,如果我正在抓取代表罗马的DECLARE @s_name VARCHAR(128) DECLARE @t_name VARCHAR(128) DECLARE db_cursor CURSOR FOR SELECT s.NAME as Schema_Nam , t.NAME as Table_Name FROM sys.tables t JOIN sys.schemas s ON t.[schema_id] = s.[schema_id] WHERE t.type = 'U' and s.name = 'schema_name' OPEN db_cursor FETCH NEXT FROM db_cursor INTO @s_name,@t_name WHILE @@FETCH_STATUS = 0 BEGIN set @sql += ' delete from ' + s.NAME + '.' + t.NAME +' Go DBCC CHECKIDENT ('''+s.NAME + '''.''' + t.NAME+''', RESEED, 0) GO ' FETCH NEXT FROM db_cursor INTO @s_name,@t_name END CLOSE db_cursor DEALLOCATE db_cursor Exec sp_executesql @sql ,那么它应该打印partnerID = 2,如果我正在爬行,则7236抓取partnerID = 37665代表柏林。

这是我的代码:

RegionID = {7236: 2, 7665: 3}

for reg in RegionID:
    page = 0
    while page <= max_pages:
        page += 1
        r = requests.get("http://www.spasso.com/affiliatesearch.aspx?&regionid=" + str(reg) + "&pid=" + str(page))
        soup = BeautifulSoup(r.content)

        g_data = soup.find_all("div", {"class": "gridHeadOuter productInfoOuter"})

        for item in g_data:
            Header = item.find_all("div", {"class": "offerInto"})
            Header_final = (Header[0].contents[0].text.strip())

        partner_ID = 2

        print("Header: " + Header_final + " | " + "PartnerID: " + partner_ID) 

这是我从上面的代码得到的输出:

Header: 1-hour Paris Sightseeing Cruise | PartnerID: 3

Header: Rome DC Open Top Hop-On Hop-Off | PartnerID: 3

理想情况下,输出将是这样的:

Header: 1-hour Paris Sightseeing Cruise | PartnerID: 3

Header: Rome DC Open Top Hop-On Hop-Off | PartnerID: 2

我不想使用if...else语句,因为我会有很多RegionIDs,我正在寻找更有效的解决方案。

更新

修改了我的代码,因为它有一些错误。根据建议创建了一个dic,但是现在已经知道如何在不使用每次if...else语句

的情况下完成它

0 个答案:

没有答案