我想从TripAdvisor推出阿姆斯特丹500家餐厅的名单; 然而,在第308家餐厅后,我收到以下错误:
Traceback (most recent call last):
File "C:/Users/dtrinh/PycharmProjects/TripAdvisorData/LinkPull-HK.py", line 43, in <module>
writer.writerow(rest_array)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 6: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我尝试了一些我在StackOverflow上找到的东西,但是现在没有任何工作.我想知道是否有人可以查看我的代码并看到任何可能的解决方案.
for item in soup2.findAll('div', attrs={'class', 'title'}):
if 'Cuisine' in item.text:
item.text.strip()
content = item.findNext('div', attrs=('class', 'content'))
cuisine_type = content.text.encode('utf8', 'ignore').strip().split(r'\xa0')
rest_array = [account_name, rest_address, postcode, phonenumber, cuisine_type]
#print rest_array
with open('ListingsPull-Amsterdam.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(rest_array)
break
Run Code Online (Sandbox Code Playgroud) 我已经用标准和遗留 SQL 编写了这个查询,但我不断收到不同的错误,从语法错误到它甚至找不到表。我在 Tableau 的自定义 SQL 数据连接器和 Web UI 中尝试过这个,并得到相同的语法错误。我正在尝试查询一年的 Google Analytics 表,但是对于标准 SQL,我收到了这样的错误:
Syntax error: Unexpected string literal '93868086.ga_sessions_*' at [1:244]
Run Code Online (Sandbox Code Playgroud)
我不太关心 Legacy SQL,因为我认为我试图查询的表不喜欢它。我只是很困惑,为什么当常见语法是将表添加为字符串时它不期望字符串。难道我做错了什么?我通常用 Legacy SQL 编写,所以如果我遗漏了什么我不会感到惊讶。任何帮助,将不胜感激。
标准 SQL:
SELECT
date,
channelGrouping,
geoNetwork.networkLocation,
device.browserVersion,
hits.dataSource,
device,
hits.page,
SUM(totals.timeOnSite),
SUM(totals.visits),
SUM(totals.bounces)
FROM
'93868086.ga_sessions_*'
WHERE
_TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY))
AND
FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
GROUP BY date
ORDER BY
date ASC
Run Code Online (Sandbox Code Playgroud) 我编写了以下代码来使用正则表达式请求页面,并查找类似于利率的字符串。整体代码有效;但是,它正在创建多个空数据帧,我无法获得删除空帧以清理输出的代码。我一直在尝试使用 .dropna、.drop 和 .empty 来尝试弃用数据帧,但输出保持不变,并继续使用我已有的信息打印空数据帧。有没有一种我不知道的方法可以摆脱这些空帧。代码和输出如下:
plcompetitors = ['https://www.lendingclub.com/loans/personal-loans',
'https://www.marcus.com/us/en/personal-loans',
'https://www.discover.com/personal-loans/']
#cycle through links in array until it finds APR rates/fixed or variable using regex
for link in plcompetitors:
cdate = datetime.date.today()
l = r.get(link)
l.encoding = 'utf-8'
data = l.text
soup = bs(data, 'html.parser')
paragraph = soup.find_all(text=re.compile('[0-9]%'))
for n in paragraph:
matches = []
matches.extend(re.findall('(?i)\d+(?:\.\d+)?%\s*(?:to|-)\s*\d+(?:\.\d+)?%', n.string))
sint = pd.Series(matches)
qdate = pd.Series([datetime.datetime.now()]*len(sint))
slink = pd.Series([link]*len(sint))
df = pd.concat([qdate,sint,slink],axis=1)
df.columns = ['Date','Interest Rate', 'URL']
print(df)
Run Code Online (Sandbox Code Playgroud)
输出:
...
0 ... …Run Code Online (Sandbox Code Playgroud) 我正在查询 365 天的 Google Analytics 数据,并将数据导出为:
20170726
Run Code Online (Sandbox Code Playgroud)
我想要的是它以某种形式解析:
2017-07-26
07/26/2017
07/26/2017
Run Code Online (Sandbox Code Playgroud)
我相信我应该使用 FORMAT_DATETIME 子句/方法来实现这一点,我有这样的:
SELECT
FORMAT_DATETIME(%m/%d/%Y, date)
Run Code Online (Sandbox Code Playgroud)
日期是 Google Analytics 中的字段。
python ×2
sql ×2
dataframe ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
regex ×1
tableau-api ×1
web-scraping ×1