I want to call 70 threads in my code as you can see to get html content :
def run_the_command(args):
conn = pyodbc.connect(
"Driver={SQL Server};"
"Server=.;"
"Database=test;"
"Trusted_Connection=yes;"
)
r = requests.get("https://coinmarketcap.com/?page=" args)
soup = BeautifulSoup(r.text, "lxml")
table = soup.find("table", class_="cmc-table")
for row in table.tbody.find_all("tr"):
# Find all data for each column
columns = row.find_all("td")
link = "https://coinmarketcap.com" columns[2].a["href"]
r2 = requests.get(link)
soup2 = BeautifulSoup(r2.text, "lxml")
cursor = conn.cursor()
sql = "insert into coins(price,submitdate,name) values(?,?,?)"
val = [
(
soup2.select_one('[class^="priceValue"]').text,
datetime.today().strftime("%Y-%m-%d"),
link,
)
]
cursor.executemany(sql, val)
conn.commit()
try:
if __name__ == "__main__":
threads = []
for i in range(70):
t = threading.Thread(target=run_the_command, args=(i 1,))
t.start()
threads.append(t)
except requests.exceptions.RequestException as e:
print(e)
But I get this error :
self._target(*self._args, **self._kwargs)
self._target(*self._args, **self._kwargs)
File "c:\Users\e.akbar\Desktop\Crypto\gettable.py", line 14, in run_the_command
File "c:\Users\e.akbar\Desktop\Crypto\gettable.py", line 14, in run_the_command
File "c:\Users\e.akbar\Desktop\Crypto\gettable.py", line 14, in run_the_command
r = requests.get('https://coinmarketcap.com/?page=' args)
TypeError: can only concatenate str (not "int") to str
r = requests.get('https://coinmarketcap.com/?page=' args)
TypeError: can only concatenate str (not "int") to str
r = requests.get('https://coinmarketcap.com/?page=' args)
r = requests.get('https://coinmarketcap.com/?page=' args)
TypeError: can only concatenate str (not "int") to str
r = requests.get('https://coinmarketcap.com/?page=' args)
TypeError: can only concatenate str (not "int") to str
TypeError: can only concatenate str (not "int") to str
I am new in python
CodePudding user response:
You would get the same error without multithreading if you call run_the_command(1)
.
You're attempting to use
to concatenate a string and a number; you could either cast the number to a string with str()
, or more modernly use an f-string to format it into your URL:
r = requests.get(f'https://coinmarketcap.com/?page={args}')
However, better yet, since you're using Requests, just use its parameter handling:
r = requests.get('https://coinmarketcap.com/', params={'page': args})
As an aside:
- you'll probably want to look into
multiprocessing
– starting 70 threads will likely not give you an appreciable performance boost due to the GIL. - you might want to use Coinmarketcap's API instead of trying to scrape things. https://coinmarketcap.com/api/
CodePudding user response:
Your issue is not related to threading. You are trying to concatenate the string with int. Try change:
r = requests.get('https://coinmarketcap.com/?page=' args)
To:
r = requests.get('https://coinmarketcap.com/?page=' str(args))