在带有Python Requests(ebay)的网站上使用搜索功能

时间:2019-05-07 02:46:36

标签: python python-requests

我正在尝试使用Requests库创建一个Python程序,该库在ebay上搜索他们输入的项目。可以使用请求库来执行Ebay搜索(或在任何网站上进行搜索),而不是对URL进行硬编码?

2 个答案:

答案 0 :(得分:0)

我认为您要在搜索元素中输入文字。根据{{​​3}}:

  

请求库是在Python中发出HTTP请求的事实上的标准。

我建议使用realpython来控制网站的源代码,例如在元素中输入文本并按网站上的按钮。

但是,如果您仍然想使用 requests ,请尝试找到处理搜索部分的api端点,并使用POST方法从中获取数据。

resp = requests.post(url)

答案 1 :(得分:0)

我创建了一个Ebay开发人员帐户来访问API,然后编写了一个小脚本来搜索eBay上某商品的历史价格。保存它的电话是search.py​​并按如下方式调用它:

./ search.py​​“您要查找的ebay项目”

您可以根据自己的喜好更改itemFilter,当前设置为solditems,自10-10-2019起。完整列表在这里:https://developer.ebay.com/devzone/finding/callref/types/ItemFilterType.html

底部的注释显示了从Ebay返回的完整字段集,您可以选择所需的字段并将其添加到打印语句中。

此外,此脚本返回的内容将比商品的第一页还要多,并且每一页每天花费您的5,000个开发人员查询中的一个。无论我如何尝试,我都无法使其与沙盒一起使用。我相信Ebay沙箱坏了。

#!/usr/local/bin/python3

from ebaysdk.finding import Connection
import sys

DEBUG = False

#search_keywords = "2019 Hot Wheels Dumbo" 
search_keywords =  sys.argv[1]
print ("Search Keywords: " + search_keywords)


# Function accepts keywords for query and pageNumber of search to pull
# Ebay will only return 100 items per search
def build_request( keywords, pageNumber):

   # Create a request structure
   # Item Filter List https://developer.ebay.com/devzone/finding/callref/types/ItemFilterType.html

   request = {
        'keywords': keywords,
        'itemFilter': [
            {'name': 'condition', 'value': 'new' ,
             'name': 'SoldItemsOnly', 'value': True ,
             'name': 'EndTimeFrom',   'value': '2019-10-10T00:00:00.000Z' }
        ],
        'paginationInput': {
            'entriesPerPage': 100, # EBay limits API Calls to 100 items per page
            'pageNumber': pageNumber
        },
        'sortOrder': 'PricePlusShippingLowest',
   }
   return (request)

# Connect using yaml file to EBAY-US production site
# put in __main__  just in case we turn this into a module later
if __name__ == '__main__':
    api = Connection(config_file='ebay.yaml', debug=False, siteid="EBAY-US")
    #api = Connection(config_file='ebay.yaml', debug=False, domain="api.sandbox.ebay.com", siteid="EBAY-US")

# Run the request
query=build_request(search_keywords, 1)
query['paginationInput']['pageNumber'] = 1
response = api.execute('findCompletedItems', query)

if DEBUG:
    print (response.dict())   #Use this to see the dictionary structure

# Display how many entries and results are returned

print("API Call: findCompletedItems")
print("----------------------------")
print(f"totalEntries: {response.reply.paginationOutput.totalEntries}, totalPages: {response.reply.paginationOutput.totalPages}")
maxpage = int(str(response.reply.paginationOutput.totalPages)) + 0


# Display item information fields from the request, see below for all possible fields
for item in response.reply.searchResult.item:
    print(f"Date: {item.listingInfo.endTime} Title: {item.title}, Price: {item.sellingStatus.currentPrice.value} Shipping: {item.shippingInfo.shippingServiceCost.value}")

# Now run the request for each page and change the page in the request each time

for page in range (2,maxpage):

    print ("**** PAGE: "+str(page) +" of "+ str(maxpage)+ " ****")
    # Rebuild the Request and Update the Page Number

    # Run the request 
    query['paginationInput']['pageNumber'] = page 
    response = api.execute('findCompletedItems', query)

    # Display item information fields from the request, see below for all possible fields
    for item in response.reply.searchResult.item:
        print(f"Date: {item.listingInfo.endTime} Title: {item.title}, Price: {item.sellingStatus.currentPrice.value} Shipping: {item.shippingInfo.shippingServiceCost.value}")


#{'ack': 'Success', 'version': '1.13.0', 'timestamp': '2019-10-16T01:28:25.891Z', 
#
#searchResult': {'item': [{'itemId': '123719989207', 'title': '2019 HOT WHEELS 2 SET CORVETTE STINGRAY SUPER CHROMES 5/5 TREASURE HUNT PAIR', 'globalId': 'EBAY-US', 'primaryCategory': {'categoryId': '180506', 'categoryName': 'Contemporary Manufacture'}, 'galleryURL': 'https://thumbs4.ebaystatic.com/m/mFuyRQgYjSutGli33dqsqcA/140.jpg', 'viewItemURL': 'https://www.ebay.com/itm/2019-HOT-WHEELS-2-SET-CORVETTE-STINGRAY-SUPER-CHROMES-5-5-TREASURE-HUNT-PAIR-/123719989207', 'paymentMethod': 'PayPal', 'autoPay': 'false', 'postalCode': '54650', 'location': 'Onalaska,WI,USA', 'country': 'US', 'shippingInfo': {'shippingServiceCost': {'_currencyId': 'USD', 'value': '6.0'}, 'shippingType': 'Flat', 'shipToLocations': 'Worldwide', 'expeditedShipping': 'false', 'oneDayShippingAvailable': 'false', 'handlingTime': '2'}, 'sellingStatus': {'currentPrice': {'_currencyId': 'USD', 'value': '9.0'}, 'convertedCurrentPrice': {'_currencyId': 'USD', 'value': '9.0'}, 'sellingState': 'Ended'}, 'listingInfo': {'bestOfferEnabled': 'false', 'buyItNowAvailable': 'false', 'startTime': '2019-04-02T22:14:03.000Z', 'endTime': '2019-10-02T18:44:49.000Z', 'listingType': 'StoreInventory', 'gift': 'false', 'watchCount': '2'}, 'returnsAccepted': 'false', 'condition': {'conditionId': '1000', 'conditionDisplayName': 'New'}, 'isMultiVariationListing': 'false', 'topRatedListing': 'false'}, 
#
#
#{'itemId': '153679182310', 'title': "Hot Wheels 2019 Super Treasure Hunt '68 Mercury Cougar Loose 1/64 STH Green", 'globalId': 'EBAY-US', 'primaryCategory': {'categoryId': '73252', 'categoryName': 'Collections & Lots'}, 'galleryURL': 'https://thumbs3.ebaystatic.com/m/mEN9EsbCJY0wb6WzXjO8hNg/140.jpg', 'viewItemURL': 'https://www.ebay.com/itm/Hot-Wheels-2019-Super-Treasure-Hunt-68-Mercury-Cougar-Loose-1-64-STH-Green-/153679182310', 'paymentMethod': 'PayPal', 'autoPay': 'false', 'location': 'Malaysia', 'country': 'MY', 'shippingInfo': {'shippingServiceCost': {'_currencyId': 'USD', 'value': '9.0'}, 'shippingType': 'Flat', 'shipToLocations': 'Worldwide', 'expeditedShipping': 'false', 'oneDayShippingAvailable': 'false', 'handlingTime': '15'}, 'sellingStatus': {'currentPrice': {'_currencyId': 'USD', 'value': '9.9'}, 'convertedCurrentPrice': {'_currencyId': 'USD', 'value': '9.9'}, 'bidCount': '1', 'sellingState': 'Ended'}, 'listingInfo': {'bestOfferEnabled': 'false', 'buyItNowAvailable': 'false', 'startTime': '2019-10-10T04:13:32.000Z', 'endTime': '2019-10-15T04:13:32.000Z', 'listingType': 'Auction', 'gift': 'false', 'watchCount': '1'}, 'returnsAccepted': 'false', 'condition': {'conditionId': '3000', 'conditionDisplayName': 'Used'}, 'isMultiVariationListing': 'false', 'topRatedListing': 'false'}], 
#
#'_count': '100'}, 'paginationOutput': {'pageNumber': '3', 'entriesPerPage': '100', 'totalPages': '40', 'totalEntries': '3966'}}