我的代码正常运行。现在我想做一些修改以从多个URL获取日期,但URL只有一个字差异。
这是我的代码,我只从一个网址获取。
from string import punctuation, whitespace
import urllib2
import datetime
import re
from bs4 import BeautifulSoup as Soup
import csv
today = datetime.date.today()
html = urllib2.urlopen("http://www.99acres.com/property-in-velachery-chennai-south-ffid").read()
soup = Soup(html)
print "INSERT INTO `property` (`date`,`Url`,`Rooms`,`place`,`PId`,`Phonenumber1`,`Phonenumber2`,`Phonenumber3`,`Typeofperson`,` Nameofperson`,`typeofproperty`,`Sq.Ft`,`PerSq.Ft`,`AdDate`,`AdYear`)"
print 'VALUES'
re_digit = re.compile('(\d+)')
properties = soup.findAll('a', title=re.compile('Bedroom'))
for eachproperty in soup.findAll('div', {'class':'sT'}):
a = eachproperty.find('a', title=re.compile('Bedroom'))
pdate = eachproperty.find('i', {'class':'pdate'})
pdates = re.sub('(\s{2,})', ' ', pdate.text)
div = eachproperty.find('div', {'class': 'sT_disc grey'})
try:
project = div.find('span').find('b').text.strip()
except:
project = 'NULL'
area = re.findall(re_digit, div.find('i', {'class': 'blk'}).text.strip())
print ' ('
print today,","+ (a['href'] if a else '`NULL`')+",", (a.string if a else 'NULL, NULL')+ "," +",".join(re.findall("'([a-zA-Z0-9,\s]*)'", (a['onclick'] if a else 'NULL, NULL, NULL, NULL, NULL, NULL')))+","+ ", ".join([project] + area),","+pdates+""
print ' ), '
以下是我想要同时获取的网址
http://www.99acres.com/property-in-velachery-chennai-south-ffid
http://www.99acres.com/property-in-thoraipakkam-chennai-south-ffid
http://www.99acres.com/property-in-madipakkam-chennai-south-ffid
因此,您可以看到每个网址中只有一个字不同。
我正在尝试创建一个类似于以下
的数组for locality in areas (http://www.99acres.com/property-in-velachery-chennai-south-ffid
, http://www.99acres.com/property-in-thoraipakkam-chennai-south-ffid, http://www.99acres.com/property-in-madipakkam-chennai-south-ffid):
link = "str(locality)"
html = urllib2.urlopen(link)
soup = Soup(html)
这似乎不起作用,我实际上只想把这个单词传递给这样的URL
for locality in areas(madipakkam, thoraipakkam, velachery):
link = “http://www.99acres.com/property-in-+ str(locality)+-chennai-south-ffid"
html= urllib2.urlopen(link)
soup = BeautifulSoup(html)
希望我说清楚
答案 0 :(得分:2)
此:
for locality in areas (http://www.99acres.com/property-in-velachery-chennai-south-ffid, http://www.99acres.com/property-in-thoraipakkam-chennai-south-ffid, http://www.99acres.com/property-in-madipakkam-chennai-south-ffid):
link = "str(locality)"
由于种种原因,......不会起作用。
首先,您正在调用一个您从未在任何地方定义过的areas
函数。而且我不确定你对这个功能的期望是什么。
其次,你试图传递http://www.99acres.com/property-in-velachery-chennai-south-ffid
,好像它是一个有意义的Python表达式,当它甚至不可解析时。如果你想传递一个字符串,你必须把它放在引号中。
第三,"str(locality)"
是文字字符串str(locality)
。如果您想在str
变量上调用locality
函数,请不要在其周围加上引号。但实际上,根本没有理由打电话给str
; locality
已经一个字符串。
最后,你没有缩进for
循环的主体。你必须缩进link =
行,以及你以前在顶级做的所有内容,以便它属于for
。这样,它对循环中的每个值都会发生一次,而不是在所有循环完成后只发生一次。
试试这个:
for link in ("http://www.99acres.com/property-in-velachery-chennai-south-ffid",
"http://www.99acres.com/property-in-thoraipakkam-chennai-south-ffid",
"http://www.99acres.com/property-in-madipakkam-chennai-south-ffid"):
# all the stuff you do for each URL
你走在正确的轨道上:
for locality in areas(madipakkam, thoraipakkam, velachery):
link = “http://www.99acres.com/property-in-+ str(locality)+-chennai-south-ffid"
使用“模板字符串”以避免重复自己几乎总是一个好主意。
但同样,还有很多问题。
首先,你再次调用了一个不存在的areas
函数,并尝试使用没有引号的裸字符串。
其次,你遇到了与上一个相反的问题:你试图将要评估的表达式+
和str(locality)
放入字符串的中间。您需要将其分解为两个单独的字符串,这些字符串可以是+
表达式的一部分。
同样,你没有缩进循环体,而且你不必要地调用str
。
所以:
for locality in "velachery", "thoraipakkam", "madipakkam":
link = “http://www.99acres.com/property-in-" + locality + "-chennai-south-ffid"
# all the stuff you do for each URL
当我们使用它时,通常使用格式化函数而不是尝试将字符串连接在一起时,更容易阅读代码,并且更容易确保没有出错。例如:
for locality in "velachery", "thoraipakkam", "madipakkam":
link = "http://www.99acres.com/property-in-{}-chennai-south-ffid".format(locality)
# all the stuff you do for each URL
在这里,很明显每个位置适合字符串的位置,字符串的外观,连字符的位置,等等。