我在这里尝试做的事情:
我正在尝试抓取 yelp 并从特定页面获取评论。但是,我只想修改此脚本以将“餐厅名称”作为输入。
例如:
用户输入: dennys-san-jose-5
URL: http://www.yelp.com/biz/**dennys-san-jose-5**
这是我现在使用的实际脚本:
from bs4 import BeautifulSoup
from urllib import urlopen
queries = 0
while queries <201:
stringQ = str(queries)
page = urlopen('http://www.yelp.com/biz/madison-square-park-new-york?start=' + stringQ)
soup = BeautifulSoup(page)
reviews = soup.findAll('p', attrs={'itemprop':'description'})
authors = soup.findAll('span', attrs={'itemprop':'author'})
flag = True
indexOf = 1
for review in reviews:
dirtyEntry = str(review)
while dirtyEntry.index('<') != -1:
indexOf = dirtyEntry.index('<')
endOf = dirtyEntry.index('>')
if flag:
dirtyEntry = dirtyEntry[endOf+1:]
flag = False
else:
if(endOf+1 == len(dirtyEntry)):
cleanEntry = dirtyEntry[0:indexOf]
break
else:
dirtyEntry = dirtyEntry[0:indexOf]+dirtyEntry[endOf+1:]
f=open("reviews.txt", "a")
f.write(cleanEntry)
f.write("\n")
f.close
for author in authors:
dirty = str(author)
closing = dirty.index('>')
dirty = dirty[closing+1:]
opening = dirty.index('<')
cleanEntry = dirty[0:opening]
f=open("bla.txt", "a")
f.write(cleanEntry)
f.write("\n")
f.close
queries = queries + 40
我正在尝试将餐厅名称读取为参数,但它无法以某种方式工作。
我做了什么:
while queries <201:
stringQ = str(queries)
page = urlopen('http://www.yelp.com/biz/' + stringQ)
但它不起作用。我将 dennys-san-jose-5作为命令行的输入(python script.py dennys-san-jose-5)
请在这里向我提出问题以及如何解决。
问候,