-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTitle.py
66 lines (49 loc) · 1.58 KB
/
getTitle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from time import sleep
import bottlenose
from bs4 import BeautifulSoup
from urllib.error import HTTPError
def error_handler(err):
ex = err['exception']
if isinstance(ex, HTTPError) and ex.code == 503:
sleep(1)
return True
def get_totalpages(amazon):
response = amazon.ItemLookup(
Service="AWSECommerceService",
Operation="ItemSearch",
Sort="date-desc-rank",
BrowseNode="3535604051",
SearchIndex="VideoDownload",
ResponseGroup="BrowseNodes,Images,ItemAttributes,ItemIds,Offers",
Keywords="映画",
)
soup = BeautifulSoup(response, "lxml")
return soup.find('totalpages').string
def get_result(amazon, cnt):
response = amazon.ItemLookup(
Service="AWSECommerceService",
Operation="ItemSearch",
Sort="date-desc-rank",
BrowseNode="3535604051",
SearchIndex="VideoDownload",
ResponseGroup="BrowseNodes,Images,ItemAttributes,ItemIds,Offers",
Keywords="映画",
ItemPage=str(cnt)
)
soup = BeautifulSoup(response, "lxml")
output_file = "result" + str(cnt) + ".html"
f = open(output_file, 'a', encoding='utf-8')
f.write(soup.prettify())
f.close()
def main():
# access_key = "xxx"
# secret_key = "xxx"
# associate_tag = "xxx"
amazon = bottlenose.Amazon(access_key, secret_key, associate_tag, Region="JP", ErrorHandler=error_handler)
totalpage = get_totalpages(amazon)
for i in range(1, int(totalpage)):
get_result(amazon, i)
sleep(1)
if __name__ == '__main__':
main()
#test()