topic description
the program that saves the crawled data from this program into the csv file
sources of topics and their own ideas
self-written program ~
related codes
import urllib.request
import urllib.parse
import json
import re
import requests
class QueryPrice(object):
def __init__(self, who, weight):
self.who = int(who)
self.weight = int(weight)
self.url = "http://www.kuaidiwo.cn/freight/ajaxjg.php"
def get_response(self):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/63.0.3239.26 QQBrowser/10.0.1125.400"
}
data = {
"expid": self.who,
"provinceid": 9, -sharpid
"cityid": 155, -sharpid
"toprovinceid": 1, -sharpid
"tocityid": 41, -sharpod
"weight": self.weight
}
html = requests.post(self.url, data, headers).text
"""
data = urllib.parse.urlencode(data, encoding="utf-8")
req = urllib.request.Request(self.url, data, headers)
resp = resp.read().decode("utf-8")
"""
return html
def get_result(self):
first = "--> %dkg:" %self.weight
html = self.get_response()
info = json.loads(html)
if info["data"] and info["jg"]:
result1 = first + "%s \t%s " % (info["jg"], info["data"][0]["name"])
return result1
else:
result2 = first + "{0} \t id={1}".format(info["jg"], self.who)
return str(result2)
if __name__ == "__main__":
prices = []
for i in range(1, 50):
q = QueryPrice(i, 20)
result = q.get_result()
price = int(re.findall(r"(\d+)\s", result)[0])
if price != 0:
print(result)
prices.append(price)
print(": %s \n: %s" % (min(prices),max(prices)))
what result do you expect? What is the error message actually seen?
you can see the programs saved in csv and the generated csv files ~