![Python Web Scraping Cookbook](https://wfqqreader-1252317822.image.myqcloud.com/cover/240/36700240/b_36700240.jpg)
上QQ阅读APP看书,第一时间看更新
How to do it
We won't parse the data in the planets.html file, but simply retrieve it from the local web server using requests:
- The following code, (found in 03/S3.py), reads the planets web page and stores it in S3:
import requests
import boto3
data = requests.get("http://localhost:8080/planets.html").text
# create S3 client, use environment variables for keys
s3 = boto3.client('s3')
# the bucket
bucket_name = "planets-content"
# create bucket, set
s3.create_bucket(Bucket=bucket_name, ACL='public-read')
s3.put_object(Bucket=bucket_name, Key='planet.html',
Body=data, ACL="public-read")
- This app will give you output similar to the following, which is S3 info telling you various facts about the new item.
{'ETag': '"3ada9dcd8933470221936534abbf7f3e"', 'ResponseMetadata': {'HTTPHeaders': {'content-length': '0', 'date': 'Sun, 27 Aug 2017 19:25:54 GMT', 'etag': '"3ada9dcd8933470221936534abbf7f3e"', 'server': 'AmazonS3', 'x-amz-id-2': '57BkfScql637op1dIXqJ7TeTmMyjVPk07cAMNVqE7C8jKsb7nRO+0GSbkkLWUBWh81k+q2nMQnE=', 'x-amz-request-id': 'D8446EDC6CBA4416'}, 'HTTPStatusCode': 200, 'HostId': '57BkfScql637op1dIXqJ7TeTmMyjVPk07cAMNVqE7C8jKsb7nRO+0GSbkkLWUBWh81k+q2nMQnE=', 'RequestId': 'D8446EDC6CBA4416', 'RetryAttempts': 0}}
- This output shows us that the object was successfully created in the bucket. At this point, you can navigate to the S3 console and see your bucket:
![](https://epubservercos.yuewen.com/02C97C/19470398001588706/epubprivate/OEBPS/Images/29fbd119-7ee5-43eb-8b2f-9bc34998ff53.png?sign=1739252828-9g32aRmT1u99HJ6ytqvZUVan8qQbM8PO-0-f8922fdd4f59f4c5c13c83e2f665f548)
The Bucket in S3
- Inside the bucket you will see the planet.html file:
![](https://epubservercos.yuewen.com/02C97C/19470398001588706/epubprivate/OEBPS/Images/49cc32c4-5ac3-4177-a397-35385afbcf4e.png?sign=1739252828-koJSoMmdoV0zEsrgvC0IL7se5JO1hQaf-0-5da7baa27b8dbadbbe322272dd4176bb)
The File in the Bucket
- By clicking on the file you can see the property and URL to the file within S3:
![](https://epubservercos.yuewen.com/02C97C/19470398001588706/epubprivate/OEBPS/Images/6c5b035d-009f-4878-9806-034b4db8e500.png?sign=1739252828-95lUGxvYELdEIUAgy1KI1dTT2JGt1QsE-0-ce05878fbcdca629f00f2765c9c246d2)
The Properties of the File in S3